python json load and dumps

python json load and dumps

Guide

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import json

str_text='{"status":0,"msg":"成功"}'
dict_json = json.loads(str_text)

print(type(dict_json)) # dict
print(dict_json)

str_pretty_result = json.dumps(
dict_json, indent=4,
sort_keys=True, ensure_ascii=False)

print(type(str_pretty_result)) # str
print(str_pretty_result)

output

<class 'dict'>
{'status': 0, 'msg': '成功'}
<class 'str'>
{
    "msg": "成功",
    "status": 0
}

str to dict
dict to str

Reference

History

  • 20191106: created.
坚持技术分享,您的支持将鼓励我继续创作!