How can I create a specific Json output for use with Highcharts?
I have a controller in ASP.net MVC outputting a JsonResult like so:
return Json(new { [...] }, JsonRequestBehavior.AllowGet);
...that looks like this:
"data":{"41_A4N1A-1":0,"41_A4N1A-2":0,"41_C4G1A-1":0,"41_C4G1A-2":0,"41_R2N1S-1":0,...
However, Highcharts' docs indicate that the data is expected like this:
"data":{"41_A4N1A-1",0},{"41_A4N1A-2",0},{"41_C4G1A-1",0},{"41_C4G1A-2",0},{"41_R2N1S-1",0},...
Note the commas instead of colons and the curly braces for each value. What .Net data structure would produce the latter results when serialized? Or, do I have to use a different library/roll my own?开发者_JAVA技巧
You could try Json.net
Use it to serialize the way you want(Make sure it can get the format right, I am sure it can) then just pass the string output to content result and set the content type, etc...
Sorry I don't have an example based on your data but it should work.
As I comented, here is a sample of how I use it in my code in a way that returns the data correctly:
return Json(new PeriodList().ToArray(), JsonRequestBehavior.AllowGet);
Although this method shouldn't exist in my code, the result comes just as expected. It also depends on the way you're dealing with your Json objects. As I read your Json result, I see that there is a property called 41_A4N1A-1
with value 0
for instance.
Also, which data structure are you using in the return statement? It could affect the result.
精彩评论