python json字典模块详解,json.dumps(),json.loads()

import json

phonebook = {'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}

with open('/media/qiao/DATA1/deep3/logs/'+'data.json','w') as fp:
    json.dump(phonebook, fp, sort_keys=True, indent=4)

打开文件得

python json字典模块详解,json.dumps(),json.loads()_第1张图片

import json
 
phonebook = {'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}

b = json.dumps(phonebook)
print(b)
print(b[0])

c = json.loads(b)
print(c)
print(c['Beth'])

{"Beth": "9102", "Alice": "2341", "Cecil": "3258"}
{
{'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}
9102

可见b是sting,c是被还原的字典。

你可能感兴趣的:(大蛇丸)