简体   繁体   English

为什么我不能解析此JSON?

[英]Why can't I parse this JSON?

I have the below JSON string: 我有以下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'}]})"

and I'm trying to create a JSON object using the code: 并且我正在尝试使用代码创建JSON对象:

var mybilljson = jQuery.parseJSON( billjson );

but the result at the console is: 但是在控制台上的结果是:

Uncaught Invalid JSON: 未捕获的无效JSON:

Why? 为什么?

String literals and property names have to use double-quotes in JSON, but you're using single quotes. 字符串文字和属性名称必须在JSON中使用双引号,但您使用的是单引号。 The parentheses aren't allowed to be there either. 括号也不允许在其中。

Remove the paranthesis in order to have valid JSON. 删除括号以拥有有效的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: () . 除了在字段名称和字符串值周围使用单引号而不是双引号之外,您的JSON字符串由于周围的parens:( ()也是无效的。

Kill the surrounding parens and change the single-quotes to double quotes: 杀死周围的括号并将单引号更改为双引号:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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