JSON: why are forward slashes escaped?

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 of {"a":"a/b/c"} .

Why?


JSON doesn't require you to do that, it allows you to do that. It also allows you to use "u0061" for "A", but it's not required. Allowing / helps when embedding JSON in a <script> tag, which doesn't allow </ inside strings, like Seb points out.

Some of Microsoft's ASP.NET Ajax/JSON API's use this loophole to add extra information, eg, a datetime will be sent as "/Date(milliseconds)/" . (Yuck)


JSON规范说你可以转义斜线,但你不必。


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#hB.3.2. The feature of the slash escape allows JSON to be embedded in HTML (as SGML) and XML.

链接地址: http://www.djcxy.com/p/1304.html

上一篇: 为什么OAuth v2具有访问权限和刷新令牌?

下一篇: JSON:为什么正斜杠会逃脱?