0

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?

Chris
2,0651 gold badge18 silver badges21 bronze badges
asked Oct 7, 2009 at 17:32

1 Answer 1

2

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.

answered Oct 7, 2009 at 17:41
1
  • Problem Solved! Thank you! I'm really new to javascript and Json... and today was a long day of searching :) Commented Oct 7, 2009 at 18:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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