javascript 带换行符的 JSON.parse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29666036/
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 with newline
提问by Juzer Ali
Why can't you parse a json with a \n
character in javascript
为什么你不能\n
在javascript中解析一个带有字符的json
JSON.parse('{"x": "\n"}')
JSON.parse('{"x": "\n"}')
However when you do JSON.parse(JSON.stringify({"x" : "\n"}))
, it is valid.
但是,当您这样做时JSON.parse(JSON.stringify({"x" : "\n"}))
,它是有效的。
http://www.jslint.com/says that {"x": "\n"}
is a valid JSON. I wonder what does spec says about this?
http://www.jslint.com/表示这{"x": "\n"}
是一个有效的 JSON。我想知道规范对此有何评论?
Update:For those who marked this duplicate, this is not the same question as "How to handle newlines in JSON". This question is more about why can't an unescaped newline character allowed in JSON.
更新:对于标记此重复的人,这与“如何处理 JSON 中的换行符”不同。这个问题更多地是关于为什么 JSON 中不允许使用未转义的换行符。
回答by philz
JSON.parse('{"x": "\n"}')
fails because '{"x": "\n"}'
is not a valid JSON string due to the unescaped slash symbol.
JSON.parse('{"x": "\n"}')
失败,因为由于'{"x": "\n"}'
未转义的斜杠符号,它不是有效的 JSON 字符串。
JSON.parse()
requires a valid JSON string to work.
JSON.parse()
需要有效的 JSON 字符串才能工作。
'{"x": "\\n"}'
is a valid JSON string as the slash is now escaped, so JSON.parse('{"x": "\\n"}')
will work.
'{"x": "\\n"}'
是一个有效的 JSON 字符串,因为斜线现在被转义了,所以JSON.parse('{"x": "\\n"}')
会起作用。
JSON.parse(JSON.stringify({"x" : "\n"}))
works because JSON.stringify internally escapes the slash character.
JSON.parse(JSON.stringify({"x" : "\n"}))
之所以有效,是因为 JSON.stringify 在内部对斜杠字符进行了转义。
The result of JSON.stringify({"x" : "\n"})
is {"x":"\n"}
but if you try to parse this using JSON.parse('{"x":"\n"})'
it will FAIL, as it is not escaped. As JSON.stringify returns an escaped character, JSON.parse(JSON.stringify())
will work.
结果JSON.stringify({"x" : "\n"})
是,{"x":"\n"}
但是如果您尝试使用JSON.parse('{"x":"\n"})'
它来解析它,它将失败,因为它没有被转义。由于 JSON.stringify 返回转义字符,因此JSON.parse(JSON.stringify())
可以使用。
回答by mohamedrias
It need to be:
它必须是:
JSON.parse('{"x": "\n"}')
You must use \\
to escape the character.
您必须使用\\
来转义字符。
Why it's invalid?
为什么无效?
It' from rfc4627 specs
它来自rfc4627 规范
All Unicode characters may be placed within the quotation marks except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F)." Since a newline is a control character, it must be escaped.
所有 Unicode 字符都可以放在引号内,但必须转义的字符除外:引号、反斜杠和控制字符(U+0000 到 U+001F)。”由于换行符是控制字符,因此它必须被逃脱。
回答by Trott
In JavaScript "\n"
is not the literal string \n
. It is a string containing a newline character. That makes your JSON invalid.
在 JavaScript"\n"
中不是文字 string \n
。它是一个包含换行符的字符串。这使您的 JSON 无效。
To represent the literal string \n
, you need to escape the initial backslash character with another backslash character: "\\n"
.
要表示文字 string \n
,您需要使用另一个反斜杠字符转义初始反斜杠字符:"\\n"
。
回答by Vikrant
You need to escape the "\
" in your string (turning it into a double-"\\
"), otherwise it will become a newline in the JSON
source, not the JSON data
.
您需要转义\
字符串中的“ ”(将其变成双“ \\
”),否则它将成为JSON
源代码中的换行符,而不是JSON data
.
Try to replace \n
with some other characters while parsing and again replace those characters with \n
before assigning this value to some control.
\n
在解析时尝试用一些其他字符替换,并\n
在将此值分配给某个控件之前再次替换这些字符。