json
JSON可包含:字符串、整型、浮点、布尔、列表、字典、NoneType
1. python数据转json字符串、json字符串转python数据
data = ['a','b','c'] str = json.dumps(data) # '["a","b","c"]' data = json.loads(str) # ['a','b','c']
dumps:是将数据转json字符串
loads:是将json字符串还原为python数据
2. 序列化
data = ['a','b','c'] file = open(fileName,'w') json.dump(data,file) file.close()
反序列化:
file = open(fileName)
data = json.load(file)
3. 数据转json字符串及序列化为文件时,中文显示为Unicode码的问题
file = open(fileName,'w',encoding='utf-8') json.dump(source,file,ensure_ascii=False)
str = json.dumps(source,ensure_ascii=False)