从来就没有救世主  也不靠神仙皇帝  要创造人类的幸福  全靠我们自己  

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)

 

posted @ 2020-07-25 17:03  T,X  阅读(112)  评论(0编辑  收藏  举报