python json.dumps 参数 ensure_ascii=False(设置utf-8)
ensure_ascii
json.dumps 序列化时对中文默认使用的ascii编码.想输出中文需要指定ensure_ascii=False(此时编码为utf-8):
>>> import json >>> sstr = json.dumps("你好") >>> print(sstr) "\u4f60\u597d" >>> ustr = json.dumps("你好", ensure_ascii=False) >>> print(ustr) "你好"
indent
indent:参数根据数据格式缩进显示,读起来更加清晰。
>>> import json >>> x = {'name':'jon','age':17,'city':'shanghai'} >>> y = json.dumps(x) #用dumps将python编码成json字符串 >>> print(y) {"city": "shanghai", "age": 17, "name": "jon"} >>> z = json.dumps(x, indent=2) >>> print(z) { "city": "shanghai", "age": 17, "name": "jon" }