javascript 如何正确声明一个json字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3992375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare a json string correctly?
提问by ajsie
So I tested two ways of declaring a json string:
所以我测试了两种声明json字符串的方法:
1:
1:
json = "{'name': 'ajsie'}";
obj = JSON.parse(json); // SyntaxError: Unexpected token ILLEGAL
2:
2:
json = '{"name": "ajsie"}';
obj = JSON.parse(json); // Worked!
What is the problem with the first one?
第一个有什么问题?
回答by SamStephens
Single quotes are not a valid quote character for strings. From http://www.json.org/: "A value can be a string in double quotes..."
单引号不是字符串的有效引号字符。来自http://www.json.org/:“值可以是双引号中的字符串......”
回答by Anon
回答by ArtBIT
Check http://www.json.org/
Strings in JSON object must be enclosed in double quotes.
JSON 对象中的字符串必须用双引号括起来。
回答by shoebox639
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. :)
http://www.json.org/是 JSON 的一个很好的参考。显然,您必须对 JSON 中的字符串使用双引号。我今天也学到了一些新东西。:)
回答by chovy
{ 'key' : 'val' }is not properly formatted json.
{ 'key' : 'val' }json 格式不正确。

