Javascript 的 JSON.parse 的错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12655517/
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
Bug with Javascript's JSON.parse?
提问by kehers
console.log(JSON.parse('{"data":"{\"json\":\"rocks\"}"}'));
gives error (tested on Firefox and Chrome's console). Is this a bug with JSON.parse? Same decodes well when tested with PHP.
给出错误(在 Firefox 和 Chrome 的控制台上测试)。这是 JSON.parse 的错误吗?使用 PHP 测试时,同样可以很好地解码。
print_r(json_decode('{"data":"{\"json\":\"rocks\"}"}', true));
回答by Felix Kling
This string is processed differently in PHP and JS, i.e. you get different results.
这个字符串在 PHP 和 JS 中的处理方式不同,即你得到不同的结果。
The only escapes sequences in single quoted strings in PHP are \\
and \'
. All others are outputted literally, according to the documentation:
PHP 中单引号字符串中唯一的转义序列是\\
和\'
。根据文档,所有其他人都按字面输出:
To specify a literal single quote, escape it with a backslash (
\
). To specify a literal backslash, double it (\\
). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as\r
or\n
, will be output literally as specified rather than having any special meaning.
要指定文字单引号,请使用反斜杠 (
\
)对其进行转义。要指定文字反斜杠,请将其加倍 (\\
)。反斜杠的所有其他实例将被视为字面反斜杠:这意味着您可能习惯的其他转义序列,例如\r
或\n
,将按照指定的字面输出,而不是具有任何特殊含义。
In JS on the other hand, if a string contains an invalid escape sequence, the backslash is discarded(CV
means character value):
另一方面,在 JS 中,如果字符串包含无效的转义序列,则丢弃反斜杠(CV
表示字符值):
- The CV of CharacterEscapeSequence :: NonEscapeCharacteris the CV of the NonEscapeCharacter.
- The CV of NonEscapeCharacter :: SourceCharacterbut not EscapeCharacteror LineTerminatoris the SourceCharactercharacter itself.
- 的CV CharacterEscapeSequence :: NonEscapeCharacter是的CV NonEscapeCharacter。
- NonEscapeCharacter :: SourceCharacter但不是EscapeCharacter或LineTerminator的 CV是SourceCharacter字符本身。
The quote might not be helpful by itself, but if you follow the link and have a look at the grammar, it should become clear.
引用本身可能没有帮助,但如果您点击链接并查看语法,它应该变得清晰。
So in PHP the string will literally contain \"
while in JS it will only contains "
, which makes it invalid JSON:
因此,在 PHP 中,字符串将字面上包含,\"
而在 JS 中,它只包含"
,这使其成为无效的 JSON:
{"data":"{"json":"rocks"}"}
If you want to create a literal backslash in JS, you have to escape it:
如果你想在 JS 中创建一个文字反斜杠,你必须转义它:
'{"data":"{\"json\":\"rocks\"}"}'
回答by I Hate Lazy
To have a literal backslash in a string literal,you need \\
.
要在字符串文字中有文字反斜杠,您需要\\
.
console.log(JSON.parse('{"data":"{\"json\":\"rocks\"}"}'));
This will successfully escape the inner quotation marks for the JSON processing.
这将成功转义 JSON 处理的内部引号。
回答by jrajav
You need to escape the backslashes:
您需要转义反斜杠:
console.log(JSON.parse('{"data":"{\"json\":\"rocks\"}"}'));?
回答by Sandeep P
object with one or more then '\'wont return Object by JSON.parser, It will return the string again with skipping one '\'. You can do parse again and again until all '\'skipped.
带有一个或多个然后'\' 的对象不会通过JSON.parser返回 Object ,它将跳过一个'\'再次返回字符串。您可以一次又一次地进行解析,直到所有'\' 都被跳过。
myobj = {\"json\":\"rocks\"}
myobj = {\"json\":\"rocks\"}
myobj = {\\"json\\":\\"rocks\\"}
myobj = {\\"json\\":\\"rocks\\"}
Following lines worked for me
以下几行对我有用
remove backslash
删除反斜杠
while(typeof myobj == 'string'){
myobj = JSON.parse(myobj)
}
回答by Oleg V. Volkov
You don't really need to escape double quotes inside single quotes and you have two extra quotes in your input around inner object, just
您真的不需要在单引号内转义双引号,并且在内部对象周围的输入中有两个额外的引号,只是
console.log(JSON.parse('{"data":{"json":"rocks"}}'));
is enough.
足够。