简体   繁体   English

为什么在JSON标准的字符串中不允许双引号和反斜杠?

[英]Why aren't double quotes and backslashes allowed in strings in the JSON standard?

If I run this in a JavaScript console in Chrome or Firebug, it works fine. 如果我在Chrome或Firebug的JavaScript控制台中运行此程序,则效果很好。

JSON.parse('"\u0027"') // Escaped single-quote

But if I run either of these 2 lines in a Javascript console, it throws an error. 但是,如果我在Javascript控制台中运行这两行中的任何一行,则会引发错误。

JSON.parse('"\u0022"') // Escaped double-quote
JSON.parse('"\u005C"') // Escaped backslash

RFC 4627 section 2.5 seems to imply that \\ and " are allowed characters as long as they're properly escaped. The 2 browsers I've tried this in don't seem to allow it, however. Is there something I'm doing wrong here or are they really not allowed in strings? I've also tried using \\" and \\\\ in place of " and \\ respectively. RFC 4627第2.5节似乎暗示只要正确转义就允许使用\\"字符。但是,我尝试过的2种浏览器似乎不允许这样做。但是,我做错了什么吗?在这里还是真的不允许它们在字符串中?我也尝试过分别使用\\"\\\\代替"\\

I feel like I'm just doing something very wrong, because I find it hard to believe that JSON would not allow these characters in strings, especially since the specification doesn't seem to mention anything that I could find saying they're not allowed. 我觉得我做错了什么,因为我很难相信JSON不允许在字符串中使用这些字符,特别是因为规范似乎没有提到我可以发现说不允许使用的任何内容。

You need to quote the backslash! 您需要引用反斜杠!

that which we call a rose 我们称之为玫瑰的

By any other name would smell as sweet 用别的名字闻起来会很甜

A double quote is a double quote, no matter how you express it in the string constant. 无论您如何在字符串常量中表达双引号,双引号都是双引号。 If you put a backslash before your \\u\u003c/code> expression within the constant, then the effect is that of a backslash-quoted double-quote, which is in fact what you've got. 如果在常量中的\\u\u003c/code>表达式前加上反斜杠,则效果就是反斜杠引号的双引号,实际上就是您所拥有的。

The most interesting thing about your question is that it helps me realize that every JavaScript string parser I've ever written is wrong. 关于您的问题最有趣的事情是,它可以帮助我认识到我编写的每个JavaScript字符串解析器都是错误的。

JavaScript is interpreting the Unicode escape sequences before they get to the JSON parser. JavaScript会在到达JSON解析器之前解释Unicode转义序列。 This poses a problem: 这带来了一个问题:

  • '"\'"' unquoted the first time (by JavaScript): "'" 第一次未用JavaScript引用'"\'"'"'"
    The second time (by the JSON parser) as valid JSON representing the string: ' 第二次(通过JSON解析器)作为有效JSON表示字符串: '

  • '"""' unquoted the first time (by JavaScript): """ 第一次未用JavaScript '"""'"""
    The JSON parser sees this unquoted version """ as invalid JSON (does not expect the last quotation mark). JSON解析器将此未加引号的版本"""视为无效JSON(不希望最后一个引号)。

  • '"\\"' unquoted the first time (by JavaScript): "\\" 第一次未用引号将'"\\"' (通过JavaScript): "\\"
    The JSON parser also sees this unquoted version "\\" as invalid JSON (second quotation mark is backslash-escaped and so does not terminate the string). JSON解析器还将未加引号的版本"\\"视为无效的JSON(第二个引号用反斜杠转义,因此不会终止字符串)。

The fix for this is to escape the escape sequence, as \\\\u.... In reality, however, you would probably just not use the JSON parser. 解决此问题的方法是转义转义序列,如\\\\u....实际上,您可能只是不使用JSON解析器。 Used in the correct context (ensured by wrapping it within parentheses, all JSON is valid JavaScript. 在正确的上下文中使用(通过将其包装在括号中可确保所有JSON是有效的JavaScript。

You are not escaping the backslash and the double-quote, as \\u00xx is being interpreted by the javascript parser. 您没有在转义反斜杠和双引号,因为\\ u00xx被javascript解析器解释。

JSON.parse('"\\\u0022"')
JSON.parse('"\\\""')

and

JSON.parse('"\\\u005C"')
JSON.parse('"\\\\"')

work as expected. 按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM