JSON: why are forward slashes escaped?

前端 未结 5 1072
一整个雨季
一整个雨季 2020-11-21 23:59

The reason for this \"escapes\" me.

JSON escapes the forward slash, so a hash {a: \"a/b/c\"} is serialized as {\"a\":\"a\\/b\\/c\"} instead

5条回答
  •  半阙折子戏
    2020-11-22 00:37

    I asked the same question some time ago and had to answer it myself. Here's what I came up with:

    It seems, my first thought [that it comes from its JavaScript roots] was correct.

    '\/' === '/' in JavaScript, and JSON is valid JavaScript. However, why are the other ignored escapes (like \z) not allowed in JSON?

    The key for this was reading http://www.cs.tut.fi/~jkorpela/www/revsol.html, followed by http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2. The feature of the slash escape allows JSON to be embedded in HTML (as SGML) and XML.

提交回复
热议问题