json.load()从文件中读取json字符串
json.loads()将json字符串转换为字典类型
json.dumps()将python中的字典类型转换为字符串类型
json.dump()将json格式字符串写到文件中
1.json.load()
with open('text.json','r',encoding='utf-8') as f :
print(json.load(f))
{ "name": "anthony", "sex": "man" }
2.json.loads()
#定义字典类型字符串
content = '{"name":"anthony","sex":"man"}'
print type(json.loads(content))
print json.load(content)
<class 'dict'>
{"name":"anthony","sex":"man"}
3.json.dumps()
content = {"name":"anthony","sex":"man"}
print type(json.dumps(content))
print json.dump(content)
<class 'str'>
'{"name":"anthony","sex":"man"}'
4.json.dump()
content = '{"name":"anthony","sex":"man"}'
with open('text.json','w',encoding='utf-8') as f:
json.dump(content,f)
"{ \"name\": \"anthony\", \"sex\": \"man\" }"
来源:oschina
链接:https://my.oschina.net/u/4364358/blog/3685016