Python json.dumps & json.loads

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/12199828.html

 

json.dumps

# dict => json str
data_dict = {
    'username' : 'hello',
    'password' : 'world'
}
result_str = json.dumps(data_dict)
type(result_str)

 

json.loads

# json str => dict
data_str = '{"username": "hello", "password": "world"}'
result_dict = json.loads(data_str)
type(result_dict)

 

Console Output

Note

dumps_data = json.dumps(result, indent=4, ensure_ascii=False)

如若需要输出格式良好的并且允许中文编码,需要额外设置indent和ensure_ascii两个参数

 

Reference

https://www.runoob.com/python3/python3-json.html

posted @ 2020-01-16 10:06  李白与酒  阅读(209)  评论(0编辑  收藏  举报