JSON

作者: ft207741 | 来源:发表于2018-08-29 06:28 被阅读0次

Menu

  • JSON定义
  • JSON方法

JSON定义
  • JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写。

JSON方法
  • json.dumps()
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()
  • json.loads()
import json
data = open("jsonfile").read()  # 打开并读取文件
mydict = json.loads(data)    # change json_str back dict
print(mydict["name"])   # Bob
  • json.dump()
import json
mydict = {"name": "Bob", "age": 17}
file = open("jsonfile", "w")
mydjson = json.dump(mydict, file)  # 把数据对象dump放进fileobj
file.close()
  • json.load()
import json
data = open("jsonfile")  # 打开文件
mydict = json.load(data)    # read and change data
print(mydict["name"])   # Bob

相关文章

网友评论

      本文标题:JSON

      本文链接:https://www.haomeiwen.com/subject/ococwftx.html