Python Json
介绍
序列化
序列化(serialization):及有序的列,数据转换成二进制的有序的过程
dump、dumps为序列化
反序列化
反序列化(deserialization):将有序的二进制序列转换成某种对象(字典,列表等)称为反序列化
load、loads为反序列化
json的使用
json.dumps()将python数据转换为json
import json data = {"name":"Kidd", "age":17, "sex":"男"} dumps = json.dumps(data) print(type(dumps))
json.loads()将json数据转换为python
import json data = {"name":"Kidd", "age":17, "sex":"男"} dumps = json.dumps(data) print(type(dumps)) loads = json.loads(dumps) print(type(loads))
json.dump()将数据写成json文件
import json data = {"name":"Kidd", "age":17, "sex":"男"} with open('data.json',"a+") as f: json.dump(data,f)
json.load()读取json文件
import json with open('data.json',"r") as f: data = json.load(f)