# -*- UTF-8 -*-
"""
@File:Json文本文件存储.py
@Description:Json模块简单使用
@Author:echohye
@Date:2022/01/28
"""
import json
data = [{
'name': 'Bob',
'gender': 'male',
'birthday': '2022-2-30'
}, {
'name': 'Mary',
'gender': 'female',
'birthday': '2022-2-28'
}]
'''
@Author:echohye
@Description:json.dumps()
@Date:2022-01-28, 周五, 19:03
'''
def dumpsToJson():
with open('dumps.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(data))
'''
@Author:echohye
@Description:josn.dump()
@Date:2022-01-28, 周五, 19:03
'''
def dumpToJson():
json.dump(data, open('dump.json', 'w'), indent=2)
'''
@Author:echohye
@Description:json.loads()
@Date:2022-01-28, 周五, 19:12
'''
def loadsToStr():
with open('dumps.json','r',encoding='utf-8') as f:
loads = json.loads(f.read())
print(loads)
'''
@Author:echohye
@Description:json.load()
@Date:2022-01-28, 周五, 19:16
'''
def loadTostr():
load = json.load(open('dump.json', 'r', encoding='utf-8'))
print(load)
if __name__ == '__main__':
dumpsToJson()
dumpToJson()
loadsToStr()
loadTostr()