python json.dumps 中文编码问题
2017-04-28 16:39
621 查看
原文链接: python json.dumps 中文编码问题
参数一旦包含中文, nodejs端校验md5码时就会失败
https://docs.python.org/3.6/library/json.html
If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped.
If ensure_ascii is false, these characters will be output as-is.
输出
python测试代码
The default is (‘, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise.
To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.
Changed in version 3.4: Use (‘,’, ‘: ‘) as default if indent is not None.
问题描述
python与nodejs通信, 参数为json格式数据, 且包含md5校验值参数一旦包含中文, nodejs端校验md5码时就会失败
原因
json.dumps参数ensure_ascii所致https://docs.python.org/3.6/library/json.html
If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped.
If ensure_ascii is false, these characters will be output as-is.
解决办法
json.dumps时设置参数
ensure_ascii=False
解决过程
nodejs测试代码md5 = require('md5'); data = {'中':'国'}; console.log(data); j = JSON.stringify(data); console.log(j); var r = md5(j); console.log(r);
输出
{ '中': '国' } {"中":"国"} d2a4c44eea271a0e00f70e4ea6fd88aa
node-md5, https://github.com/pvorb/node-md5
python测试代码
➜ ~ python3 Python 3.5.2 (default, Sep 14 2016, 11:28:32) [GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import json >>> import hashlib >>> _dict = {'中': '国'} >>> _json = json.dumps(_dict, separators=(',', ':')) >>> _json '{"\\u4e2d":"\\u56fd"}' >>> _json.encode('utf-8') b'{"\\u4e2d":"\\u56fd"}' >>> m = hashlib.md5() >>> m.update(_json.encode('utf-8')) >>> m.hexdigest() '84d64021339907e604f1dff60f505c14' >>> _json = json.dumps(_dict, separators=(',', ':'), ensure_ascii=False) >>> _json '{"中":"国"}' >>> _json.encode('utf-8') b'{"\xe4\xb8\xad":"\xe5\x9b\xbd"}' >>> m = hashlib.md5() >>> m.update(_json.encode('utf-8')) >>> m.hexdigest() 'd2a4c44eea271a0e00f70e4ea6fd88aa'
其他补充
json.dumps参数separators, 如果不设置为separators=(‘,’, ‘:’)也会导致nodejs端校验md5码失败The default is (‘, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise.
To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.
Changed in version 3.4: Use (‘,’, ‘: ‘) as default if indent is not None.
相关文章推荐
- python json.dumps 中的ensure_ascii 参数引起的中文编码问题
- python中json数据中文编码显示的问题
- python json.dumps() 中文乱码问题
- Python下调用json.dumps中文显示问题解决办法
- python json 序列化反序列化 和 中文编码问题
- Python下调用json.dumps中文显示问题及解决办法
- python json.dumps输出中文问题
- python 读写Json的中文编码问题
- Python下调用json.dumps中文显示问题解决办法
- Python下调用json.dumps中文显示问题解决办法
- python中文编码与json中文输出问题详解
- Python读写json文件中文编码问题
- Python下调用json.dumps中文显示问题解决办法
- python的json.dumps中文问题
- python json.dumps 中文编码
- python json.dumps() 中文乱码问题
- python中文编码问题
- python+mysql 操作数据库出现的中文编码问题
- python的中文编码问题
- 利用python处理xml -- 中文编码问题