json.dumps用于将dict转为为json字符串:
import json
data = {
"hi" : "hello",
"who" : "world"
}
json_str = json.dumps(data)
print(json_str)
运行程序输出:
{"hi": "hello", "who": "world"}
如果希望输出的字符串可以自动缩进,以更便于阅读,可以使用参数indent:
import json
data = {
"hi" : "hello",
"who" : "world"
}
json_str = json.dumps(data, indent=4) //缩进4个空格
print(json_str)
运行程序输出:
{
"hi": "hello",
"who": "world"
}
如果python对象中有中文,直接转换会输出unicode的编码值:
import json
data &