2009-10-07 85 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