Python JSON 序列化以及反序列化 文件读写

发布于:2024-03-15 ⋅ 阅读:(63) ⋅ 点赞:(0)

Python JSON 序列化以及反序列化

  • JSON (JavaScript Object Notation) 是一种轻量级的文本数据存储格式。
  • JSON 数据通常存储在字符串中,即JSON字符串,其实就是一字符串,只是带有一定的格式,可以被解析。
  • 本文使用的 Python 版本为3.12

反序列化

  • JSON字符串解析为Python对象叫做JSON的反序列化,也叫做JSON的解码

  • 反序列化一般使用json模块的loadsload方法。

  • loads当中的s并不是复数的意思,而是指处理的对象类型为str bytes 和 bytearray

  • load方法处理的对象类型为file like obj

  • 以上两个方法根据输入数据的不同,返回不同的Python对象。具体的转换关系见下表:

    JSON 字符串 Python 对象
    object dict
    array list
    string str
    number(integer) int
    number(real) float
    false False
    true True
    null None

字符串的反序列化

  • str bytes 和 bytearray的反序列化,使用方法loads

    # -*- coding:utf-8 -*-
    import json
    
    json_obj_str = '{"number": 88888888, "名字": "小明"}'
    # encode 方法:将 字符串 转为 bytes
    # decode 方法,将 bytes 转为 字符串
    json_obj_bytes = json_obj_str.encode(encoding='UTF-8')
    json_array_str = '[1, 2, 3, "hello"]'
    json_str = '"hello"'
    json_int_num_str = '6666'
    json_float_num_str = '888.888'
    json_true_str = 'true'
    json_false_str = 'false'
    json_none_str = 'null'
    
    def json_str_decode(arg):
        python_obj = json.loads(arg)
        print(f'value: {python_obj}, type: {type(python_obj)}')
    
    for tmp in [json_obj_str, json_obj_bytes, json_array_str, json_str, json_int_num_str, json_float_num_str,
                json_true_str, json_false_str, json_none_str]:
        json_str_decode(tmp)
    
    '''
    输出为:
    value: {'number': 88888888, '名字': '小明'}, type: <class 'dict'>
    value: {'number': 88888888, '名字': '小明'}, type: <class 'dict'>
    value: [1, 2, 3, 'hello'], type: <class 'list'>
    value: hello, type: <class 'str'>
    value: 6666, type: <class 'int'>
    value: 888.888, type: <class 'float'>
    value: True, type: <class 'bool'>
    value: False, type: <class 'bool'>
    value: None, type: <class 'NoneType'>
    '''
    

json 文件的反序列化

  • 文件的反序列化,使用方法load

    # -*- coding:utf-8 -*-
    import json
    
    '''
    test.json 文件内容如下:
    {
      "名字": "小明",
      "number": 888888,
      "女朋友": null
    }
    '''
    
    # 当 json 文件中含有中文时,得指定编码为 UTF-8
    with open('test.json', 'r', encoding='UTF-8') as f:
        python_obj = json.load(f)
        print(f'value: {python_obj}, type: {type(python_obj)}')
    
    '''
    输出为:
    value: {'名字': '小明', 'number': 888888, '女朋友': None}, type: <class 'dict'>
    '''
    

序列化

  • Python对象转为JSON字符串叫做JSON的序列化,也叫做JSON的编码
  • 序列化一般使用json模块的dumpsdump方法。
  • dumps当中的s并不是复数的意思,而是指字符串,即将Python对象编码为字符串
  • dump方法将Python对象编码为字符串并写入file like obj中。

Python 对象的序列化

  • Python对象的序列化,使用方法dumps

    # -*- coding:utf-8 -*-
    import json
    
    py_obj_dict = {"number": 88888888, "名字": "小明"}
    py_obj_array = [1, 2, 3, "hello"]
    py_obj_str = 'hello'
    py_obj_int = 6666
    py_obj_float = 888.888
    py_obj_true = True
    py_obj_false = False
    py_obj_none = None
    
    def json_str_encode(arg):
        # 当包含中文时,需指定 ensure_ascii=False
        json_str = json.dumps(arg, ensure_ascii=False)
        print(f'value: {json_str}, type: {type(json_str)}')
    
    
    for tmp in [py_obj_dict, py_obj_array, py_obj_str, py_obj_int, py_obj_float,
                py_obj_true, py_obj_false, py_obj_none]:
        json_str_encode(tmp)
    
    '''
    输出为:
    value: {"number": 88888888, "名字": "小明"}, type: <class 'str'>
    value: [1, 2, 3, "hello"], type: <class 'str'>
    value: "hello", type: <class 'str'>
    value: 6666, type: <class 'str'>
    value: 888.888, type: <class 'str'>
    value: true, type: <class 'str'>
    value: false, type: <class 'str'>
    value: null, type: <class 'str'>
    '''
    

json 文件的序列化

  • 文件的序列化,使用方法dump

    # -*- coding:utf-8 -*-
    import json
    
    py_obj_dict = {'名字': '小明', 'number': 888888, '女朋友': None}
    
    # 当包含中文时,须同时指定  encoding='UTF-8' 以及 ensure_ascii=False
    with open('test.json', 'w', encoding='UTF-8') as f:
        # indent=2 会使得输出更加优美
        json.dump(py_obj_dict, f, ensure_ascii=False, indent=2)
    
本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到