我应该如何在JSON中转义字符串? [英] How should I escape strings in JSON?

查看:119
本文介绍了我应该如何在JSON中转义字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

手动创建JSON数据时,如何转义字符串字段?我应该使用Apache Commons Lang的 StringEscapeUtilities.escapeHtml StringEscapeUtilities.escapeXml 之类的东西,还是应该使用 java.net.URLEncoder

When creating JSON data manually, how should I escape string fields? Should I use something like Apache Commons Lang's StringEscapeUtilities.escapeHtml, StringEscapeUtilities.escapeXml, or should I use java.net.URLEncoder?

问题是当我使用 SEU.escapeHtml ,它不会转义引号,当我将整个字符串包装在一对'中时,将生成格式错误的JSON。

The problem is that when I use SEU.escapeHtml, it doesn't escape quotes and when I wrap the whole string in a pair of 's, a malformed JSON will be generated.

推荐答案

理想情况下,找到您所用语言的JSON库,您可以将一些适当的数据结构提供给并让它担心如何逃避事情。它会让你更加理智。如果由于某种原因你没有使用你的语言库,你不想使用它(我不会建议这个¹),或者你正在编写一个JSON库,请继续阅读。

Ideally, find a JSON library in your language that you can feed some appropriate data structure to, and let it worry about how to escape things. It'll keep you much saner. If for whatever reason you don't have a library in your language, you don't want to use one (I wouldn't suggest this¹), or you're writing a JSON library, read on.

根据RFC逃脱它。 JSON非常自由:必须逃脱的唯一字符是 \ ,和控制代码(任何小于U + 0020的东西)。

Escape it according to the RFC. JSON is pretty liberal: The only characters you must escape are \, ", and control codes (anything less than U+0020).

这种转义结构特定于JSON。你需要一个特定于JSON的函数。所有的转义都可以写成 \uXXXX 其中 XXXX 是该字符的UTF-16代码单元¹。有几个快捷方式,例如 \\ ,也可以。(并且它们会产生更小更清晰的输出。)

This structure of escaping is specific to JSON. You'll need a JSON specific function. All of the escapes can be written as \uXXXX where XXXX is the UTF-16 code unit¹ for that character. There are a few shortcuts, such as \\, which work as well. (And they result in a smaller and clearer output.)

有关详细信息,请参阅 RFC

For full details, see the RFC.

¹JSON的转义是基于JS构建的,所以它使用 \uXXXX ,其中 XXXX 是一个UTF-16代码单元。对于BMP之外的代码点,这意味着编码代理对,这可能会有点毛茸茸。(或者,您可以直接输出字符,因为JSON的编码是Unicode文本,并允许这些标准特征字符。)

¹JSON's escaping is built on JS, so it uses \uXXXX, where XXXX is a UTF-16 code unit. For code points outside the BMP, this means encoding surrogate pairs, which can get a bit hairy. (Or, you can just output the character directly, since JSON's encoded for is Unicode text, and allows these particular characters.)

这篇关于我应该如何在JSON中转义字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆