0

what i need to do is load my pie data chart created with extjs4 with data from my database the store accept this data ,so i need to return my data in format that look like this:

var data = [{ name: "Low", data1: "20", data2: "54", data3: "63", data4: "12" },
 { name: "Moderate", data1: "2", data2: "74", data3: "13", data4: "25" },
 { name: "Critical", data1: "42", data2: "17", data3: "3", data4: "20" },
 { name: "High", data1: "25", data2: "14", data3: "23", data4: "52"}];

so im trying to do this in c# with json.net, i created a class.cs and put my queries to get the data i need :

namespace charts
{
public class lineChartClass
{
    public String piedata()
    {     
{//..my queries in here..//}
   double[] data = new double[4] ;
        //data = "{ name: \"Low\", data1: " + lowtotal + "}" + ",{ name: \"Moderate\", data1: " + moderatetotal + "}" + ",{ name: \"Critical\", data1: " + criticaltotal + "}" + ",{ name: \"High\", data1: " + hightotal + "}";
       data[0]=lowtotal;
       data[1] = moderatetotal;
       data[2] = criticaltotal;
       data[3] = hightotal;
        return data;



    }//eo piedata
}
    public class ChartItem
    {
        public string Name { get; set; }

        public string Data1 { get; set; }


    }

}

and created my handler.ashx to convert to json:

namespace charts {
public class lineChartData : IHttpHandler
{
    static string ConvertToJson()
    {
     List<ChartItem> chartItems = new List<ChartItem>();

        chartItems.Add(new ChartItem() { Name = "Low", Data1 = json[0].ToString() });
        chartItems.Add(new ChartItem() { Name = "Moderate", Data1 = json[1].ToString() });
        chartItems.Add(new ChartItem() { Name = "Critical", Data1 = json[2].ToString() });
        chartItems.Add(new ChartItem() { Name = "High", Data1 = json[3].ToString() });
        string result = new JavaScriptSerializer().Serialize(chartItems);
        return result;

    }
} 
}

then in my chart.js i call it like this:

var obj= new lineChartData();

window.store1 = Ext.create('Ext.data.JsonStore', {
fields: ['name', 'dat1', 'data2', 'data3', 'data4'],
data: obj.ConvertToJson()//generateData()
});

but i get this error:

Uncaught ReferenceError: lineChartData is not defined
lineChartData.ashxGET http://localhost/lineChartData.ashx?proxy 500 (Internal Server Error)

im just starting with c# and extjs and dont know how to use json.net

thanks in advance for ur time

3
  • The code catch (Exception ex) { throw ex; } is evil: don't catch exceptions you cannot handle or wrap; and never rethrow exceptions. Commented Jul 15, 2011 at 14:46
  • i added it after i got the error,removing it dosnt change anything,thx Commented Jul 15, 2011 at 14:51
  • It changes the stack-trace and thus makes debugging harder. Commented Jul 15, 2011 at 16:17

2 Answers 2

3

Why don't you use standard versions of JSON serialization tools at both client-side and server-side. ASP.NET has JavaScriptSerializer class & JavaScript has JSON object (native in ECMAScript 5 and a plugin for older versions, written by Douglas Crockford).

To convert and Object to JSON at server, you can use:

string objectJson = new JavaScriptSerializer().Serialize(yourObject);

To serialize object to JSON at client-side, you can use:

string objectJson = JSON.stringify(yourObject);

To deserialize a JSON string into an object in client-side, you can use:

var yourObject = JSON.parse(objectJson);

Thus you can have a class to represent your chart item at server:

public class ChartItem
{
    public string Name {get;set;}

    public string Data1 {get;set;}

    public string Data2 {get;set;}

    public string Data3 {get;set;}

    public string Data4 {get;set;}
}

and use it in your `ConvertToJson' method as follow:

List<ChartItem> chartItems = new List<ChartItem>();
// add as many item as you wish to this list
chartItems.Add(new ChartItem(){ Name = "Something", Data1 = "data1", Data2 = "data2", Data3 = "data3", Data4 = "data4 });
string result = new JavaScriptSerialize().Serialize(chartItems);
return result;
Sign up to request clarification or add additional context in comments.

6 Comments

i dont mind, i just need it to work any way possible,and if its easier and u can point me to how to do it that would be great
Well, JSON has been around for many years and thousands of developers has used it. Therefore its framework is completely mature both server-side and client-side. See update :)
ok.so where do i add this in my code? replace the converttojson() with JavaScriptSerializer().Serialize()? plz bear with me,total newbie here
JsonSerializer isn't as robust nor feature-complete as Json.NET, however. If mis-serializes the null character as the constant null (for instance) and it discards time-zone info from serialized DateTime structures. So using the library isn't such an odd choice, particularly when you consider that it enables somewhat cleaner calling code.
u meant JavaScriptSerializer() not JavaScriptSerialize()
|
1

Have you added them in your web.config properly:

<httpHandlers>
  <add verb="GET" path="lineChartData.ashx" type="charts.lineChartData"/>
</httpHandlers>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.