加载中...

Json模块

Json模块

序列化/反序列化模块

import json

json格式的数据,所有的编程语言都能识别,本身是字符串
类型有要求: int float bool str list tuple dict None

json 主要应用于传输数据 , 序列化成字符串
pickle 主要应用于存储数据 , 序列化成二进制字节流

dumps 和 loads

dic = {"name":"梁新宇","sex":"野味","age":22,"family":["爸爸","妈妈","姐姐"]}
"""ensure_ascii=False 显示中文 sort_keys=True 按键排序"""
dic = {"name":"梁新宇","sex":"野味","age":22,"family":["爸爸","妈妈","姐姐"]}
res = json.dumps(dic,ensure_ascii=False,sort_keys=True)
print(res , type(res))  #out type is str

dic = json.loads(res)
print(dic , type(dic))   #out type is dic

dump 和 load

with open("lianxi3.json",mode="w",encoding="utf-8") as fp:
	json.dump(dic,fp,ensure_ascii=False)
with open("lianxi3.json",mode="r",encoding="utf-8") as fp:
	dic = json.load(fp)
print(dic , type(dic))
posted @ 2024-03-04 00:37  江寒雨  阅读(27)  评论(0)    收藏  举报