views:

65

answers:

1

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" : "65000","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?

A: 

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: http://json.org/json2.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.

quixoto
Problem Solved!Thank you! I'm really new to javascript and Json... and today was a long day of searching :)
Andrei T. Ursan