2012-09-16 99 views
1

我有以下JSON字符串的:爲什麼我不能解析這個JSON?

var billjson = "({'posts' : [{'Id' :'  7553','Code' :'1186 ','Address' :' GGGG 39Α     ','Name' : ' GGGG NAME 3        ','Description' : ' G 3 ','EntrySeason' : ' GGGGG 08-09  ','Period' : ' 10/2009 ','Revenue' : '  4.10'},{'Id' :'  7553','Code' :'1186 ','Address' :' GGGG 39Α     ','Name' : ' FFFF NAME 3        ','Description' : ' F 3 ','EntrySeason' : ' FFFF 08-09  ','Period' : ' 10/2009 ','Revenue' : '  4.10'}]})" 

,我嘗試使用代碼來創建一個JSON對象:

var mybilljson = jQuery.parseJSON(billjson); 

但在控制檯上的結果是:

未捕獲無效JSON:

爲什麼?

回答

6

字符串文字和屬性名稱必須在JSON中使用雙引號,但是您使用的是單引號。括號也不允許在那裏。

1

除了在字段名稱和字符串值周圍使用單引號而不是雙引號外,由於周圍的parens,您的JSON字符串無效:()

殺死周圍的括號,改變單引號雙引號:

var billjson = '{"posts" : [{"Id" :"  7553","Code" :"1186 ", ... 
3

纔能有有效的JSON取出paranthesis。您也必須使用雙引號:

var billjson= '{"posts": [{"Id": "7553","Code": "1186","Address": "GGGG39Α","Name": "GGGGNAME3","Description": "G3","EntrySeason": "GGGGG08-09","Period": "10/2009","Revenue": "4.10"},{"Id": "7553","Code": "1186","Address": "GGGG39Α","Name": "FFFFNAME3","Description": "F3","EntrySeason": "FFFF08-09","Period": "10/2009","Revenue": "4.10"}]}'; 
var mybilljson = jQuery.parseJSON(billjson); 
相關問題