我有一个这样的字符串:

s = u"""{"desc": "\u73cd\u54c1\u7f51-\u5168\u7403\u6f6e\u6d41\u5962\u54c1\u7f51\u7edc\u96f6\u552e\u5546 <br \/>\r\nhttp:\/\/www.zhenpin.com\/ <br \/>\r\n<br \/>\r\n200\u591a\u4e2a\u56fd\u9645\u4e00\u7ebf\u54c1\u724c\uff0c\u9876\u7ea7\u4e70\u624b\u5168\u7403\u91c7\u8d2d\uff0c100%\u6b63\u54c1\u4fdd\u969c\uff0c7\u5929\u65e0\u6761\u2026"}"""

json.loads(s) returns error message like this:

ValueError: Invalid control character at: line 1 column 33 (char 33)

为什么会出现这种错误?我怎样才能解决这个问题?

推荐答案

问题是unicode字符串在JSON数据中包含回车符(\r)和换行符(\n)within字符串文字.如果它们是字符串本身的一部分,那么它们应该被适当地转义.如果它们不是字符串的一部分,那么它们也不应该出现在JSON中.

If you can't fix where you got this JSON string to produce valid JSON, you could either remove the offending characters:

>>> json.loads(s.replace('\r\n', ''))

or escape them manually:

>>> json.loads(s.replace('\r\n', '\\r\\n'))

Json相关问答推荐

Jolt Transformation—如果子对象为空,则将父对象更新为空

如何使用JQ有条件 Select 值

Allof Indide的JSON模式之一

使用 jq 和脚本 bash 映射两个 json

在 PostgreSQL 中 Select 分层 JSON 作为表

是否可以在有条件的情况下将 json 对象转换为 JOLT 中的数组?

使用 KQL 和外部 data() 运算符从 json 文件中提取信息

Kotlin Android Room 处理 Moshi TypeConverter 中的空对象列表

如果 jq 数组中的字符串是对象或字符串,则获取值

Servicestack 返回数组而不是带有数组的对象

如何使用 CORS 实现 JavaScript Google Places API 请求

一起使用 Argparse 和 Json

在 bash 中将 CSV 转换为 JSON

如何获取json格式的KendoGrid显示数据?

jQuery JSON 响应总是触发 ParseError

从 JSON 创建 Hashtable

[__NSCFNumber 长度]:发送到实例 UITableView 的无法识别的 Select 器

无法将空值放入 JSON 对象

春天:返回@ResponseBodyResponseEntity>

添加json文件注释