zl程序教程

您现在的位置是:首页 >  其他

当前栏目

关于json.dumps中的参数详解

2023-03-20 15:27:43 时间

例子如下:

import json
import numpy as np


class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.ndarray):
            return obj.tolist()
        elif isinstance(obj, bytes):
            return str(obj, encoding='utf-8');
        return json.JSONEncoder.default(self, obj)


dict = {'id': 1, 'title': b'xe7xacxacxe4xb8x80xe7xabxa0 xe7xa7xa6xe7xbexbd'}
dup = json.dumps(dict, cls=MyEncoder, ensure_ascii=False, indent=4)
print(dup)

indent

根据数据格式缩进显示,读起来更加清晰,indent的数值,代表缩进的位数。

ensure_ascii

如果无任何配置,或者说使用默认配置, 输出的会是中文的ASCII字符吗,而不是真正的中文。 这是因为json.dumps 序列化时对中文默认使用的ascii编码。

{
    "id": 1,
    "title": "u7b2cu4e00u7ae0 u79e6u7fbd"
}

cls

dict类型的数据(存在中文),在python2中是可以转化的,但是在python3中存在序列化问题:

TypeError: Object of type bytes is not JSON serializable