简体   繁体   中英

Why can't I parse this JSON?

I have the below JSON string:

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'}]})"

and I'm trying to create a JSON object using the code:

var mybilljson = jQuery.parseJSON( billjson );

but the result at the console is:

Uncaught Invalid JSON:

Why?

String literals and property names have to use double-quotes in JSON, but you're using single quotes. The parentheses aren't allowed to be there either.

Remove the paranthesis in order to have valid JSON. Also you must use double quotes:

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 );

In addition to using single-quotes rather than double-quotes around field names and string values, your JSON string is invalid because of the surrounding parens: () .

Kill the surrounding parens and change the single-quotes to double quotes:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM