Asp.Net MVC returning a Json from a string
Funny as it can sound, I transformed my datatable data into a string which looks something like this:
{ blocks: [
{"fromAge" : "60","fromAmount" : "0","toAge" : "64","toAmount" : "650开发者_JAVA技巧00","color" : "red","orderNo" : "2"},
{"fromAge" : "66","fromAmount" : "0","toAge" : "72","toAmount" : "12000","color" : "red","orderNo" : "4"},
{"fromAge" : "64","fromAmount" : "0","toAge" : "72","toAmount" : "34400","color" : "red","orderNo" : "1"},
{"fromAge" : "64","fromAmount" : "19500","toAge" : "66","toAmount" : "50750","color" : "red","orderNo" : "3"}
]}
with the ideea that "blocks" is something like an array with 4 dicitionaries, then with this action I return the string to the View:
public ActionResult ChartDetails()
{
return Json(GetJson(datatable));
}
GetJson(DataTable dt) is the method which returns the string, and in view I get this:
"{ blocks: [{\"fromAge\" : \"60\",\"fromAmount\" : \"0\",\"toAge\" : \"64\",\"toAmount\" : \"65000\",\"color\" : \"Color [Orange]\",\"orderNo\" : \"2\"}, {\"fromAge\" : \"66\",\"fromAmount\" : \"0\",\"toAge\" : \"72\",\"toAmount\" : \"12000\",\"color\" : \"Color [Green]\",\"orderNo\" : \"4\"}, {\"fromAge\" : \"64\",\"fromAmount\" : \"0\",\"toAge\" : \"72\",\"toAmount\" : \"34400\",\"color\" : \"Color [Blue]\",\"orderNo\" : \"1\"}, {\"fromAge\" : \"64\",\"fromAmount\" : \"19500\",\"toAge\" : \"66\",\"toAmount\" : \"50750\",\"color\" : \"Color [Pink]\",\"orderNo\" : \"3\"}]}"
And because of all the \" and "}" and " I cannot read my Json with JQuery, is there some method to return a string with a normal encoding in my case?
The string you're seeing is JSON, but properly encoded as a Javascript string. You still need to parse it in Javascript into objects to use it with jQuery.
See the "official" JSON parser from json.org here: https://github.com/douglascrockford/JSON-js If you have that in your environment, you can do this:
var jsonString = /* my string from the GetJson call */
var data = JSON.parse(jsonString);
And then you should be able to use "data" as a proper list of maps.
精彩评论