JSON库将json对象或节点转换成字典、将json数组转换成列表、将json字符串转换成python字符串,true和false转换成True和False,null转换为None
将json对象转化为Python可处理的形式:
import json
jsonstring='{"user_man":[{"name":"xie"},{"name":"lu"}],"user_woman":[{"name":"xie2013"},{"name":"lu2013"}]}'
json_data=json.loads(jsonstring)
print(json_data["user_man"]) # [{'name': 'xie'}, {'name': 'lu'}]
print(json_data["user_man"][0]["name"]) # xie
将字典转换为json对象:
import json
data={"name":"xieyabo","age":23,"password":123123}
json_str=json.dumps(data)
json.load()和json.dump()用来处理文件:
写入 JSON 数据
with open('data.json', 'w') as f:
json.dump(data.json, f)
读取数据
with open('data.json', 'r') as f:
data = json.load(f)