Python Json序列化与反序列化

来源:互联网 发布:安卓让软件防止卸载 编辑:程序博客网 时间:2024/04/28 07:02
在python中,序列化可以理解为:把python的对象编码转换为json格式的字符串,反序列化可以理解为:把json格式字符串解码为python数据对象在python的标准库中,专门提供了json库与pickle库来处理这部分。

  json的dumps方法和loads方法,可实现数据的序列化和反序列化。具体来说,dumps方法,可将json格式数据序列为Python的相关的数据类型;loads方法则是相反,把python数据类型转换为json相应的数据类型格式要求。在序列化时,中文汉字总是被转换为unicode码,在dumps函数中添加参数ensure_ascii=False即可解决。

 

  下面是json的序列化与反序列化:

  1、Json序列化如下:

1 import json2 print (json.__all__)    #查看json库的所有方法3 ['dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONEncoder']

  未在dumps函数中添加参数ensure_ascii=False,结果如下:

复制代码
 1 #coding: utf-8 2 import json 3  4 dict = {'name':'zhangsan', 'age':33, 'address':'红星路'} 5 print('未序列化前的数据类型为:', type(dict)) 6 print('为序列化前的数据:', dict) 7 #对dict进行序列化的处理 8 dict_xu = json.dumps(dict)    #直接进行序列化 9 print('序列化后的数据类型为:', type(dict_xu))10 print('序列化后的数据为:', dict_xu)11 12 ----------------------------------------------------------------13 未序列化前的数据类型为: <class 'dict'>14 为序列化前的数据: {'name': 'zhangsan', 'address': '红星路', 'age': 33}15 序列化后的数据类型为: <class 'str'>16 序列化后的数据为: {"name": "zhangsan", "address": "\u7ea2\u661f\u8def", "age": 33}
复制代码

  在dumps函数中添加参数ensure_ascii=False,结果如下:

 

复制代码
 1 #coding: utf-8 2 import json 3  4 dict = {'name':'zhangsan', 'age':33, 'address':'红星路'} 5 print('未序列化前的数据类型为:', type(dict)) 6 print('为序列化前的数据:', dict) 7 #对dict进行序列化的处理 8 dict_xu = json.dumps(dict,ensure_ascii=False)    #添加ensure_ascii=False进行序列化 9 print('序列化后的数据类型为:', type(dict_xu))10 print('序列化后的数据为:', dict_xu)11 -------------------------------------------------------------------12 未序列化前的数据类型为: <class 'dict'>13 为序列化前的数据: {'address': '红星路', 'age': 33, 'name': 'zhangsan'}14 序列化后的数据类型为: <class 'str'>15 序列化后的数据为: {"address": "红星路", "age": 33, "name": "zhangsan"}
复制代码

 

  2、Json反序列化如下:

复制代码
 1 #coding: utf-8 2 import json 3  4 dict = {'name':'zhangsan', 'age':33, 'address':'红星路'} 5 print('未序列化前的数据类型为:', type(dict)) 6 print('为序列化前的数据:', dict) 7 #对dict进行序列化的处理 8 dict_xu = json.dumps(dict,ensure_ascii=False)    #添加ensure_ascii=False进行序列化 9 print('序列化后的数据类型为:', type(dict_xu))10 print('序列化后的数据为:', dict_xu)11 #对dict_xu进行反序列化处理12 dict_fan = json.loads(dict_xu)13 print('反序列化后的数据类型为:', type(dict_fan))14 print('反序列化后的数据为: ', dict_fan)15 ----------------------------------------------------------------------16 未序列化前的数据类型为: <class 'dict'>17 为序列化前的数据: {'name': 'zhangsan', 'age': 33, 'address': '红星路'}18 序列化后的数据类型为: <class 'str'>19 序列化后的数据为: {"name": "zhangsan", "age": 33, "address": "红星路"}20 反序列化后的数据类型为: <class 'dict'>21 反序列化后的数据为:  {'name': 'zhangsan', 'age': 33, 'address': '红星路'}
复制代码

 

   在实际的工作中,序列化或者反序列化的可能是一个文件的形式,不可能像如上写的那样简单的,下来就来实现这部分,把文件内容进行序列化和反序列化,先来看序列化的代码,两步操作:1、先序列化 列表对象 ;2、步把序列化成的字符串写入文件:

复制代码
 1 #coding: utf-8 2 import json 3  4 list = ['Apple','Huawei','selenium','java','python'] 5 #把list先序列化,写入到一个文件中 6 # 两步操作 1步先序列化 列表对象  2步把序列化成的字符串写入文件 7 json.dump(list, open('e:/test.txt','w'))    8 r1=open('e:/test.txt','r') 9 print(r1.read())10 -------------------------------------------------------------------11 ["Apple", "Huawei", "selenium", "java", "python"]
复制代码

反序列化,两步操作:1、先读取文件的字符串对象;2、然后反序列化成列表对象:

复制代码
 1 #coding: utf-8 2 import json 3  4 list = ['Apple','Huawei','selenium','java','python'] 5 #把list先序列化,写入到一个文件中 6 # 两步操作 1步先序列化 列表对象  2步把序列化成的字符串写入文件 7 json.dump(list, open('e:/test.txt','w'))    8 r1=open('e:/test.txt','r') 9 print(r1.read())10 #------------------------------------------------------------11 #两步操作:1、先读取文件的字符串对象;2、然后反序列化成列表对象12 res=json.load(open('e:/test.txt','r'))13 print (res)14 print('数据类型:',type(res))15 -------------------------------------------------------------16 ["Apple", "Huawei", "selenium", "java", "python"]17 ['Apple', 'Huawei', 'selenium', 'java', 'python']18 数据类型: <class 'list'>
0 0