JSON

2018-08-29  本文已影响0人  ft207741

Menu


JSON定义

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
上一篇 下一篇

猜你喜欢

热点阅读