json.dumps(a, indent=5, ensure_ascii=False)
ensure_ascii表示的意思是是否要转为ASCII码,如果打开(默认打开True),那么转为json后中文会变成ASCII编码,如果关闭后中文还是中文,不会变为ASCII编码。
例如2:
import json friends={"name":"王虎","name1":"张二","name2":"姚晨"} print(json.dumps(friends))
执行结果:
例如2:
friends={"name":"王虎","name1":"张二","name2":"姚晨"} print(json.dumps(friends,ensure_ascii=False))
执行结果:
indent表示间隔的长度,使python数据类型在序列化的同时,保持json格式优化:
例如1:不加indent参数
例如2:加indent参数
python之反序列化json.dumps()函数输出json格式,使用indent参数对输出进行json格式优化,使用ensure_ascii参数对中文输入的支持
原文:https://www.cnblogs.com/hls-code/p/14888036.html