相关疑难解决方法(0)

将json.dumps中的utf-8文本保存为UTF8,而不是\ u转义序列

示例代码:

>>> import json
>>> json_string = json.dumps("??? ????")
>>> print json_string
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"
Run Code Online (Sandbox Code Playgroud)

问题是:它不是人类可读的.我(智能)用户想要使用JSON转储验证甚至编辑文本文件.(我宁愿不使用XML)

有没有办法将对象序列化为utf-8 json字符串(而不是\ uXXXX)?

这没有帮助:

>>> import json
>>> json_string = json.dumps("??? ????")
>>> print json_string
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"
Run Code Online (Sandbox Code Playgroud)

工作,但如果任何子对象是python-unicode而不是utf-8,它将转储垃圾:

>>> import json
>>> json_string = json.dumps("??? ????")
>>> print json_string
"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"
Run Code Online (Sandbox Code Playgroud)

python unicode json escaping utf-8

394
推荐指数
11
解决办法
29万
查看次数

为什么json.dumps使用"\ uxxxx"转义非ascii字符

在Python 2中,该函数json.dumps()将确保将所有非ascii字符转义为\uxxxx.

Python 2 Json

但这不是很混乱,因为它\uxxxx是一个unicode字符,应该在unicode字符串中使用.

输出json.dumps()是a str,它是Python 2中的字节字符串.因此它不应该将字符转义为\xhh

>>> unicode_string = u"\u00f8"
>>> print unicode_string
ø
>>> print json.dumps(unicode_string)
"\u00f8"
>>> unicode_string.encode("utf8")
'\xc3\xb8'
Run Code Online (Sandbox Code Playgroud)

python unicode json python-2.x

3
推荐指数
1
解决办法
5193
查看次数

标签 统计

json ×2

python ×2

unicode ×2

escaping ×1

python-2.x ×1

utf-8 ×1