zoukankan      html  css  js  c++  java
  • Python Json序列化与反序列化

      在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 json
    2 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": "u7ea2u661fu8def", "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'>
  • 相关阅读:
    程序员自我【营销】,如何打造个人【品牌】
    程序员应该怎样和领导相处?
    程序员必备能力——晋升之道
    聊一聊 软件系统中的“热力学第二定律”
    程序员如何利用技术管理技巧
    技术人必须掌握能力——深度思考
    程序员逆袭之路——系列文章更新中
    程序员跳槽,该如何选择一家好公司
    C++-运行时类型信息,异常(day11)
    C++-多态,纯虚函数,抽象类,工厂模式,虚析构函数(day10)
  • 原文地址:https://www.cnblogs.com/diaosicai/p/6419833.html
Copyright © 2011-2022 走看看