python中的 json 模块使用

时间:2023-03-09 18:53:14
python中的 json 模块使用

(1)python 中生成 json 字符串:

import json
data = dict(ret=0, msg="Welcome, Login success!")
json_str = json.dumps(data, sort_keys=True)
print json_str

(2)python 中解析 json字符串:

import json
json_str = '{"msg": "Welcome, Login success!", "ret": 0}'
data_dict = json.loads(json_str)
print data_dict