通过 json 实现序列化
方法1
>>>import json
>>>d = dict(name='Bob', age=20, score=88)
>>>json.dumps(d)
'{"age": 20, "score": 88, "name": "Bob"}'
方法2
dump()方法可以直接把JSON写入一个file-like Object。
#coding:utf-8
import json
a =dict(name='admin',type="index")
with open("123.txt","w") as ff:
json.dump(a,ff)
none@None:~$ cat 123.txt
{"name": "admin", "type": "index"}
通过json 实现反序列化
#coding:utf-8
import json
with open("11.json","r") as ff:
a = json.load(ff)
完!