Skip to main content
 首页 » 编程设计

python之无法使用单个未转义的反斜杠将 json 存储在 python 中

2024年09月07日7虾米姐

我正在为 REST 负载主体创建一个 json 主体,如下所示:

>>> j = json.loads('["foo", {"bar": ["to_be_replaced", 1.1, 1.0, 2]}]') 
>>> text = "aaaa" + "\\" + "bbbbb" + "\\" + "cccc" 
>>> j[1]["bar"][0] = text 
>>> j 
['foo', {'bar': ['aaaa\\bbbbb\\cccc', 1.1, 1.0, 2]}] 

烦人的是,对方期望的格式是这样的

"aaaa\bbbb\cccc".  

我知道这是个糟糕的主意。

我已经尝试了所有方法,并且开始相信将这种格式的文本存储在 json 对象中是根本不可能的。有办法吗?或者我是否需要让网络服务的开发人员选择更合理的分隔符。

我知道它真的是一个反斜杠,如果我打印一个反斜杠

>>> print(text) 
aaaa\bbbbb\cccc 

但这并不能帮助我把它变成一个 json 对象。

请您参考如下方法:

是的,它是不可能的——设计使然。

JSON 解析器本质上应该只发出有效的 JSON。来自 RFC 8259 ,强调我的:

7. Strings

The representation of strings is similar to conventions used in the C family of programming languages. A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks, except for the characters that MUST be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F).

Any character may be escaped. If the character is in the Basic Multilingual Plane (U+0000 through U+FFFF), then it may be represented as a six-character sequence: a reverse solidus, followed by the lowercase letter u, followed by four hexadecimal digits that encode the character's code point. The hexadecimal letters A through F can be uppercase or lowercase. So, for example, a string containing only a single reverse solidus character may be represented as "\u005C".

Alternatively, there are two-character sequence escape representations of some popular characters. So, for example, a string containing only a single reverse solidus character may be represented more compactly as "\\".


请注意短语“必须转义”——“必须”是一个正式定义的技术术语;不符合 JSON 规范的 MUST 要求的东西不允许自称为 JSON。

总结:数据中仅包含文字反斜杠的字符串可能在 JSON 中编码为 "\u005c" 或 "\\"。它不能编码为"\"(包括该字符作为未转义文字)。