2009-10-07 59 views
0

有趣,因为它听起来返回JSON,我改变了我的数据表的数据到一个字符串,它看起来是 这样的:Asp.Net MVC从字符串

{ 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"} 
]} 

与IDEEA说,“块”是什么像4个dicitionaries阵列,然后用这个动作我的字符串返回给查看:

public ActionResult ChartDetails() 
{  
     return Json(GetJson(datatable)); 
} 

的getJSON(数据表DT)是它返回字符串的方法中,并鉴于我得到这样的:

"{ 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\"}]}" 

由于所有的“和”}“和”我无法用JQuery读取JSON,有没有一种方法可以在我的情况下返回一个正常编码的字符串?

回答

2

你看到的字符串是JSON,但正确编码为Javascript字符串。你仍然需要在Javascript中将它解析为对象,以便在jQuery中使用它。

见“官方”的JSON解析器json.org这里:https://github.com/douglascrockford/JSON-js 如果你有在你的环境,你可以这样做:

var jsonString = /* my string from the GetJson call */ 
var data = JSON.parse(jsonString); 

然后你应该能够使用“数据”作为适当的地图列表。

+0

问题解决了! 谢谢!我对JavaScript和Json非常陌生...今天是漫长的一天搜索:) – andreiursan 2009-10-07 18:13:38