开发者

How to declare a json string correctly?

So I tested two ways of declaring a json string:

1:

json = "{'name': 'ajsie'}";
obj = JSON.parse(json); // SyntaxError: Unexpe开发者_如何转开发cted token ILLEGAL

2:

json = '{"name": "ajsie"}';
obj = JSON.parse(json); // Worked!

What is the problem with the first one?


Single quotes are not a valid quote character for strings. From http://www.json.org/: "A value can be a string in double quotes..."


json.org defines a string to use " instead of '. That's my guess.


Check http://www.json.org/

Strings in JSON object must be enclosed in double quotes.


http://www.json.org/ is a great reference for JSON. Apparently you have to use double quotes for strings in JSON. I learned something new today too. :)


{ 'key' : 'val' } is not properly formatted json.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜