pickle 和 shevle 序列化后得到的数据只有python才能够解析
通常企业开发不可能做一个单机程序 都需要联网进行计算机间的交互
我们必须保证这个数据能够跨平台使用
JSON是什么? java scrip object notation
var obj = {"name" :"egon"}
对于我们开发而言 json 就是一种通用的数据格式 任何语言都能解析
js 中的数据类型 python数据类型 的对应关系
{} 字典
[] 列表
string “ ” str
int/float int/float
true/false True/False
null None
json 格式的语法规范
最外层通常是一个字典或列表
{} or []
只要你想写一个json格式的数据 那么最外层直接写{}
字符串必须是双引号
你可以在里面套人一多的层次
json模块的核心功能
dump
dumps
load
loads
不带s 封装write 和read
import json # 反序列化 # with open("a.json","rt",encoding="utf-8") as f: # res = json.loads(f.read()) # print(type(res)) # with open("a.json",encoding="utf-8") as f: # print(json.load(f)) # 直接解析字符串的json为python对象 jsontext = """{ "users": [{ "name": "agon", "age": 68 }, { "name": "agon", "age": 68 } ] }""" # res = json.loads(jsontext) # print(res) mydic = { "users": [{ "name": "agon", "age": 68 }, { "name": "agon", "age": 68 } ] } # with open("b.json","wt",encoding="utf-8") as f: # f.write(json.dumps(mydic)) # with open("b.json", "wt", encoding="utf-8") as f: # json.dump(mydic, f) import json # dic = {"a": '理查德姑妈', "b": "找到你", "c": "看不见的客人"} # with open("c.json","wt",encoding="utf-8") as f: # f.write(json.dumps(dic)) # print(repr(s), type(s)) # with open("c.json","rt",encoding="utf-8") as f: # # print(f.read()) # d = json.loads(f.read()) # print(d)