javascript JSON.parse 错误给出了双引号字符串的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22101353/
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
JSON.parse error is giving error with double quotes string
提问by ????? ?????
Why is it that
为什么是这样
//Code
JSON.parse("{'name':'Khushal Khan'}");
results in this error
导致这个错误
//Resposnse
SyntaxError: Unexpected token '
while this works perfectly
虽然这很完美
//Code
JSON.parse('{"name":"Khushal Khan"}');
Output:
输出:
//Response
Object {name: "Khushal Khan"}
回答by Nevett
The problem is the type of quote used in your JSON string, not the outer quotes. The JSON specification only allows for double quoted strings. You could use either type of quote to actually pass your JSON string to the parse()
function, though.
问题在于 JSON 字符串中使用的引号类型,而不是外部引号。JSON 规范只允许双引号字符串。不过,您可以使用任一类型的引号将 JSON 字符串实际传递给parse()
函数。
From the JSON spec:
从JSON 规范:
回答by Quentin
The problem is not that your JavaScript string uses "
characters but that your JSON strings do not.
问题不在于您的 JavaScript 字符串使用"
字符,而在于您的 JSON 字符串没有。
JSON is not JavaScript. JSON strings must be delimited by "
characters.
JSON 不是 JavaScript。JSON 字符串必须由"
字符分隔。
From the specification:
从规范:
string = quotation-mark *char quotation-mark
and
和
quotation-mark = %x22 ; "
回答by SajithNair
Problem is not with double quoted string, but the json should have no single quotes as delimiters.
问题不在于双引号字符串,而是 json 应该没有单引号作为分隔符。