python json dump/load/dumps/loads

load

从外部JSON文件变成dict字典(外部文件一定要是json格式)
json.load(open('a.json',"r"))

dump

把dict字典变成json格式,生成到外部文件里面
json.dump(dict,open('a.json',"w"))

dumps

Convert Python Object (Dict) to JSON

1
2
3
4
5
6
7
8
import json

d = {}
d["Name"] = "Luke"
d["Country"] = "Canada"

print json.dumps(d, ensure_ascii=False)
# result {"Country": "Canada", "Name": "Luke"}

loads

To convert JSON to a Python dict use this:

1
2
3
4
5
6
import json

json_data = '{"name": "Brian", "city": "BJ"}'
python_obj = json.loads(json_data)
print python_obj["name"]
print python_obj["city"]

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!