JSON:为什么要转义正斜杠? [英] JSON: why are forward slashes escaped?
问题描述
这个逃避"我的原因.
JSON 转义了正斜杠,所以散列 {a: "a/b/c"}
被序列化为 {"a":"a/b/c"}
而不是 {"a":"a/b/c"}代码>.
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"}
.
为什么?
推荐答案
JSON 不需要您这样做,它允许您这样做.它还允许您使用u0061"对于A",但这不是必需的,就像 Harold L 指出的那样:
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, like Harold L points out:
JSON 规范说您可以转义正斜杠,但您不必这样做.
The JSON spec says you CAN escape forward slash, but you don't have to.
Harold L 于 09 年 10 月 16 日 21:59 回复
允许 /
在将 JSON 嵌入 <script>
标签时有帮助,这不允许 </
在字符串中,就像 Seb 指出的那样:
Allowing /
helps when embedding JSON in a <script>
tag, which doesn't allow </
inside strings, like Seb points out:
这是因为 HTML 不允许 <script>
标签内的字符串包含 </
,所以如果子字符串在那里,你应该转义每个正斜杠.
This is because HTML does not allow a string inside a
<script>
tag to contain</
, so in case that substring's there, you should escape every forward slash.
Seb 于 09 年 10 月 16 日 22:00 回复 (#1580667)
某些 Microsoft 的 ASP.NET Ajax/JSON API 使用此漏洞添加额外信息,例如,日期时间将作为 "/Date(milliseconds)/"
发送.(呸)
Some of Microsoft's ASP.NET Ajax/JSON API's use this loophole to add extra information, e.g., a datetime will be sent as "/Date(milliseconds)/"
. (Yuck)
这篇关于JSON:为什么要转义正斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!