最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why aren't double quotes and backslashes allowed in strings in the JSON standard? - Stack Overflow

programmeradmin1浏览0评论

If I run this in a JavaScript console in Chrome or Firebug, it works fine.

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

But if I run either of these 2 lines in a Javascript console, it throws an error.

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 \u0022 and \u005C respectively.

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.

If I run this in a JavaScript console in Chrome or Firebug, it works fine.

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

But if I run either of these 2 lines in a Javascript console, it throws an error.

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 \u0022 and \u005C respectively.

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.

Share Improve this question edited Oct 7, 2021 at 5:54 CommunityBot 11 silver badge asked Jan 2, 2011 at 2:42 Dan HerbertDan Herbert 103k51 gold badges192 silver badges221 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

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 expression within the constant, then the effect is that of a backslash-quoted double-quote, which is in fact what you've got.

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 is interpreting the Unicode escape sequences before they get to the JSON parser. This poses a problem:

  • '"\u0027"' unquoted the first time (by JavaScript): "'"
    The second time (by the JSON parser) as valid JSON representing the string: '

  • '"\u0022"' unquoted the first time (by JavaScript): """
    The JSON parser sees this unquoted version """ as invalid JSON (does not expect the last quotation mark).

  • '"\u005C"' unquoted the first time (by 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).

The fix for this is to escape the escape sequence, as \\u.... In reality, however, you would probably just not use the JSON parser. Used in the correct context (ensured by wrapping it within parentheses, all JSON is valid JavaScript.

You are not escaping the backslash and the double-quote, as \u00xx is being interpreted by the javascript parser.

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

and

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

work as expected.

发布评论

评论列表(0)

  1. 暂无评论