Menu
JSON定义
- JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写。
JSON方法
import json
mydict = {"name": "Bob", "age": 17}
myjson = json.dumps(mydict) #dict changed json_str
file = open("jsonfile", "w")
file.write(myjson) # json_str write into file
file.close()
import json
data = open("jsonfile").read() # 打开并读取文件
mydict = json.loads(data) # change json_str back dict
print(mydict["name"]) # Bob
import json
mydict = {"name": "Bob", "age": 17}
file = open("jsonfile", "w")
mydjson = json.dump(mydict, file) # 把数据对象dump放进fileobj
file.close()
import json
data = open("jsonfile") # 打开文件
mydict = json.load(data) # read and change data
print(mydict["name"]) # Bob