当前位置: 首页 > news >正文

不用开源程序怎么做网站徐州建设工程材料检测预约网站

不用开源程序怎么做网站,徐州建设工程材料检测预约网站,公司网站制作企业,个人网站备案地址Python JSON 序列化以及反序列化 JSON (JavaScript Object Notation) 是一种轻量级的文本数据存储格式。JSON 数据通常存储在字符串中,即JSON字符串,其实就是一字符串,只是带有一定的格式,可以被解析。本文使用的 Python 版本为3…

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 对象
    objectdict
    arraylist
    stringstr
    number(integer)int
    number(real)float
    falseFalse
    trueTrue
    nullNone

字符串的反序列化

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

    # -*- coding:utf-8 -*-
    import jsonjson_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 jsonpy_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 = Nonedef json_str_encode(arg):# 当包含中文时,需指定 ensure_ascii=Falsejson_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 jsonpy_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)
    
http://www.yayakq.cn/news/571078/

相关文章:

  • 湖南网站建设大全襄阳谷城网站开发
  • 实施网站推广的最终目的专业的单位网站开发公司
  • 阿里云免费建站园林景观设计公司计划书
  • 焦作网站建设的公司哪家好上传了网站标志 功能链接
  • 网站空间的分类wordpress前台登录主题
  • 怎么查网站有没有做404汕头网上推广公司
  • 网站优化提升速度网络营销常见术语
  • ps 矢量素材网站网页设计师工作职责
  • 怎么做网站的外部连接如何做网站的导航栏
  • 网站首页被k 做跳转加盟平台网站怎么做
  • 企业网站设计软件网站后台管理方便吗
  • oracle数据库做的网站拓客app下载
  • 珠海微网站进入网页设计图片链接跳转代码
  • 苏州个人网站建设个人备案转企业网站期间
  • 想给公司注册一个网站南京网站制作千
  • 网站建设业务元提成针对315老坛酸菜企业解决方案
  • 莱西网站建设astro wordpress
  • 传奇做网站有网打不开网页咋回事
  • 企业网站如何做推广萧山网站建设xsszwl
  • 微网站怎么做的好名字魔方 网站建设 有限公司
  • 网站更换主机html手机版网站
  • 企业建设网站的过程和预算表安卓aso优化工具
  • 网站小视频怎么做代理商网站后台文章编辑不了
  • app定制开发网站有哪些网站开发建设价格附件
  • 创建网站的向导和模板可以做彩页的网站
  • 怎样建设单位网站idc网站备案
  • 手机网站策划书方案长安镇网站建设
  • 重庆专业网站推广路桥贝斯特做网站好吗
  • 网站空间域名续费世界上有几个空间站
  • 邵阳网站建设优化为什么要做响应式网站