这个很简单,就两方法,我写了两个函数,做个小笔记
# -----------------------
# __Author : tyran
# __Date : 17-11-6
# -----------------------
import json
def serialize(obj):
return json.dumps(obj) # 序列化
def un_serialize(obj):
return json.loads(obj) # 读取序列化
if __name__ == '__main__':
d = {'aaa': 111, 'bbb': 222}
json_data = serialize(d)
with open('../log/jsonTest.txt', 'w') as fd:
fd.write(json_data)
# ---------------------------------
with open('../log/jsonTest.txt', 'r') as fd:
data = fd.read()
data = un_serialize(data)
print(data['aaa'])