javascript - JSON:为什么正斜杠转义?

这个“逃脱”我的原因。

JSON转义正斜杠,因此哈希 {a: "a/b/c"} 被序列化为 {"a":"a\/b\/c"} 而不是 {"a":"a/b/c"}.

为什么?

最佳答案

JSON 不要求您这样做,它允许您这样做。它还允许您将“\u0061”用于“A”,但这不是必需的,就像 Harold L 指出的那样:

The JSON spec says you CAN escape forward slash, but you don't have to.

Harold L 于 2009 年 10 月 16 日 21:59 回答

允许 \/在将 JSON 嵌入到 <script> 中时会有所帮助标签,不允许</在字符串内部,就像 Seb 指出的那样:

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 于 2009 年 10 月 16 日 22:00 回答 (#1580667)

Microsoft 的一些 ASP.NET Ajax/JSON API 使用此漏洞添加额外信息,例如,日期时间将作为 "\/Date(milliseconds)\/" 发送. (呸)

https://stackoverflow.com/questions/1580647/

相关文章:

ruby - 在 Ruby 中解析 JSON 字符串

json - 安装 json gem 时出错 'mkmf.rb can' t find header

java - 我应该将 jackson 的 ObjectMapper 声明为静态字段吗?

javascript - 如何以编程方式美化 JSON?

json - 在 JSON 中表示 null

json - 如何将 JSON 对象转换为 TypeScript 类?

javascript - 在对象数组中查找属性的最大值

ios - 在 iOS 中从 NSDictionary 生成 JSON 字符串

ruby-on-rails - 如何使用 Ruby on Rails 解析 JSON?

ruby-on-rails - 如何将 ruby​​ 哈希对象转换为 JSON?