# -*- coding: utf-8 -*
"""没有嵌套类的类 author: Jill usage: """
import json class Leaf:
def __init__(self, leaf_arg_a, leaf_arg_b):
self.leaf_arg_a = leaf_arg_a
self.leaf_arg_b = leaf_arg_b def __str__(self):
return (
"Leaf:\n"
"arg_a: {0.leaf_arg_a}\n"
"arg_b: {0.leaf_arg_b}\n"
).format(self) def leaf2dict(self):
return {
'leafArgA': self.leaf_arg_a,
'leafArgB': self.leaf_arg_b
} def dict2leaf(self):
return Leaf(self['leafArgA'], self['leafArgB']) @staticmethod
def parse_json_obj(json_str):
a = json.loads(json_str, object_hook=Leaf.dict2leaf)
return a def to_json_str(self):
return json.dumps(self, default=Leaf.leaf2dict) if __name__ == '__main__':
leaf = Leaf("a", "b")
print(leaf.to_json_str()) print() json_str = '{"leafArgA": "a", "leafArgB": "b"}'
json_obj = Leaf.parse_json_obj(json_str)
print(json_obj.leaf_arg_a)
"""有嵌套类Leaf的类

author: Jill

usage:
"""
import json from entity.child import Leaf class Root:
def __init__(self, root_arg_a, leafs):
self.root_arg_a = root_arg_a
self.leafs = leafs def root2dict(self):
return {
'rootArgA': self.root_arg_a,
'leafs': json.loads(json.dumps(self.leafs, default=Leaf.leaf2dict)),
} def to_json_str(self):
return json.dumps(self, default=Root.root2dict) @staticmethod
def from_json_obj_get(key, json_str):
if key == 'leafs':
json_array = json.dumps(json.loads(json_str).get(key))
array = json.loads(json_array, object_hook=Leaf.dict2leaf)
return array
return json.loads(json_str).get(key) @staticmethod
def parse_json_obj(json_str):
json_dict = json.loads(json_str)
root_arg_a = json_dict.get('rootArgA')
leafs = Leaf.load_from_java(json.dumps(json_dict.get('leafs')))
return Root(root_arg_a, leafs) if __name__ == '__main__':
leaf1 = Leaf('a1', 'b1')
leaf2 = Leaf('a2', 'b2')
leafs = [leaf1, leaf2]
root = Root("root_a", leafs) json_obj = root.to_json_str()
print(json_obj) json_str = '{"rootArgA": "root_a", "leafs": ' \
'[{"leafArgA": "a1", "leafArgB": "b1"}, {"leafArgA": "a2", "leafArgB": "b2"}]}'
root_obj = Root.parse_json_obj(json_str)
for leaf in root_obj.leafs:
print(leaf) print(root_obj.root_arg_a)
leaf_list = Root.from_json_obj_get('leafs', json_str)
print(leaf_list[0].leaf_arg_a)

Python序列化和反序列化vsJSON的更多相关文章

  1. Python序列化和反序列化

    Python序列化和反序列化 通过将对象序列化可以将其存储在变量或者文件中,可以保存当时对象的状态,实现其生命周期的延长.并且需要时可以再次将这个对象读取出来.Python中有几个常用模块可实现这一功 ...

  2. python序列化与反序列化(json与pickle)

    在python中,序列化可以理解为将python中对象的编码格式转换为json(pickle)格式的字符串,而反序列化可以 理解为将json(pickle)格式的字符串转换为python中对象的编码格 ...

  3. Python—序列化和反序列化模块(json、pickle和shelve)

    什么是序列化 我们把对象(或者变量)从内存中变为可存储或者可传输的过程称为序列化.在python中为pickling,在其他语言中也被称之为serialization,marshalling,flat ...

  4. Python 序列化与反序列化

    序列化是为了将内存中的字典.列表.集合以及各种对象,保存到一个文件中(字节流).而反序列化是将字节流转化回原始的对象的一个过程. json库 序列化:json.dumps() 反序列化:json.lo ...

  5. python序列化与反序列化(json、pickle)-(五)

    1.什么是序列化&反序列化? 序列化:将字典.列表.类的实例对象等内容转换成一个字符串的过程. 反序列化:将一个字符串转换成字典.列表.类的实例对象等内容的过程 PS:Python中常见的数据 ...

  6. python 序列化和反序列化

    概念 序列化: 将对象的状态信息转换为可以存储或传输的形式的过程.就是把对象转换成字符串的过程 反序列化: 把字符串转换成python可以识别的数据类型对象的过程 应用 #数据存储 #网络传输 模块 ...

  7. python 序列化,反序列化

    附: pickle 有大量的配置选项和一些棘手的问题.对于最常见的使用场景,你不需要去担心这个,是如果你要在一个重要的程序中使用pickle 去做序列化的话,最好去查阅一下官方文档. https:// ...

  8. Python序列化与反序列化-json与pickle

    Python序列化与反序列化-json与pickle 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.json的序列化方式与反序列化方式 1>.json序列化 #!/usr ...

  9. Python库:序列化和反序列化模块pickle介绍

    1 前言 在“通过简单示例来理解什么是机器学习”这篇文章里提到了pickle库的使用,本文来做进一步的阐述. 通过简单示例来理解什么是机器学习 pickle是python语言的一个标准模块,安装pyt ...

随机推荐

  1. HDOJ 1061 Rightmost Digit

    找出数学规律 原题: Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  2. Arpa’s obvious problem and Mehrdad’s terrible solution 思维

    There are some beautiful girls in Arpa’s land as mentioned before. Once Arpa came up with an obvious ...

  3. 多边形面积问题(hdoj2036)

    杭电oj2036http://acm.hdu.edu.cn/showproblem.php?pid=2036 计算几何,求多边形的面积 只要记住这个公式: 如果逆时针给出点坐标,值为正, 如果顺时针给 ...

  4. Mysql_connect报告”No such file or directory”错误的解决方法

    写了个php脚本单独执行mysql_connect(),发现错误信息居然是“No such file or directory"! 首先确定是mysql_connect()和mysql_pc ...

  5. ZH奶酪:【Python】random模块

    Python中的random模块用于随机数生成,对几个random模块中的函数进行简单介绍.如下:random.random() 用于生成一个0到1的随机浮点数.如: import random ra ...

  6. Windows nginx php cgi-fcgi 配置 xdebug

    之前使用的是 Apache + PHP,不用怎么配置就可以. 由于服务器用的是 nginx,为了和服务器一致,所以本地开发也改为 nginx. 开始只是简单的开启 xdebug, 发现并不行. 找了一 ...

  7. spring boot中,jar包、war包的区别

    jar包.war包 比较(表格) 项目 jar包 war包 在开发调试完成之后,可以将应用打成JAR包的形式,可以直接使用Maven插件的package命令,最终会形成一个可运行的 JAR包.我们使用 ...

  8. JSON.stringify、JSON.parse、toJSON 区别

    JSON.stringify 方法 将一个 JavaScript 值转换为一个 JSON 字符串 可以将数组.对象等转换后的 JSON 字符串,保存在 sessionStorage.localStor ...

  9. centos6安装GitLab全程详解和常见问题解决

    GitLab,是一个使用 Ruby on Rails 开发的开源应用程序,与Github类似,能够浏览源代码,管理缺陷和注释,非常适合在团队内部使用. 官方只提供了Debian/Ubuntu系统下的安 ...

  10. C语言实现<读取>和<写入> *.ini文件(转)

    原地址:https://blog.csdn.net/niha1993825jian/article/details/41086403 #include <stdio.h> #include ...