JSON 模块
用法,与pickle一样。
import json
d = {
"name":"chen",
"role":"police",
"blood": 76,
"weapon":"ak47"
}
alive_palyers = ['alex','jack','rain']
print(json.dumps(d))
print(type(json.dumps(d)))
f = open("game.json",'w')
json.dump(d,f)
#json.dump(alive_palyers,f) #多次dump后,load时会报错
-------------------------------------------------------------
{"name": "chen", "role": "police", "blood": 76, "weapon": "ak47"}
<class 'str'>
import json
f = open("game.json","r")
d = json.load(f)
print(d["role"])
----------------
police
pickle 与json 区别
pickle仅支持python,支持py中所有数据类型 class->object,function,datetime,
json支持所有语言,仅支持常规数据类型 str,int,dict,set,list,tuple