Python 之 Json序列化嵌套类方式
作者:IT-蓝月 时间:2021-05-25 18:11:44
想要用python自已手动序列化嵌套类,就要明白两个问题:
1.Json是什么?
2.Json支持什么类型?
答案显而易见
Json就是嵌套对象
Json在python中支持列表,字典(当然也支持int,string.....,不过说这个也没多大必要)
很好,等等,列表,字典?我们在python中学过列表,字典,字典列表,列表字典,字典字典,那,我们可不可以把类对象转化为这些呢?
我可以很确定的告诉你,可以,并且,嵌套类都可以!!!
下面就来实战:
from flask import Flask
import json
app = Flask(__name__)
class City():
def __init__(self,country,provider):
self.country = country
self.provider = provider
class School():
def __init__(self,country,provider,name,nums):
self.city = City(country,provider)
self.name = name
self.nums = nums
@app.route('/method0')
def method0():
school = School('china','shanxi','wutaizhongxue','2000')
s_temp0 = [school.city.country,school.city.provider,school.name,school.nums]
return json.dumps(s_temp0)
@app.route('/method1')
def method1():
school = School('china','shanxi','wutaizhongxue','2000')
s_temp1 = {'country':school.city.country,'provider':school.city.provider,'name':school.name,'nums':school.nums}
return json.dumps(s_temp1)
@app.route('/method2')
def method2():
school = School('china','shanxi','wutaizhongxue','2000')
s_temp2 = [{'country':school.city.country,'provider':school.city.provider},school.name,school.nums]
return json.dumps(s_temp2)
@app.route('/method3')
def method3():
school = School('china','shanxi','wutaizhongxue','2000')
s_temp3 = {'city':[school.city.country,school.city.provider],'name':school.name,'nums':school.nums}
return json.dumps(s_temp3)
@app.route('/method4')
def method4():
school = School('china','shanxi','wutaizhongxue','2000')
s_temp4 = {'city':{'country':school.city.country,'provider':school.city.provider},'name':school.name,'nums':school.nums}
return json.dumps(s_temp4)
if __name__ == '__main__':
app.run(debug=True)
执行效果:
很多人会说,第五种才是我想要的,前面四种不是标准的json数据,刚开始确实是这样认为的,但是。。。
1.如果你处理的两个嵌套类是数据库的呢? * 如一对多的关系型数据库,method3不是一个很好的选择么?
2.如果你处理的两个嵌套类是包含关系呢?method2不是一个很好的选择么?
来源:https://blog.csdn.net/ITlanyue/article/details/81207104
标签:Python,Json,序列化,嵌套类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
教你用python控制安卓手机
2023-11-10 10:13:39
![](https://img.aspxhome.com/file/2023/4/69554_0s.png)
PHP实现生成Excel文件并导出的示例详解
2023-05-28 13:11:18
关于Python网络爬虫框架scrapy
2023-03-17 17:02:50
通过Python 获取Android设备信息的轻量级框架
2021-08-18 21:41:00
python求平均数、方差、中位数的例子
2022-08-23 21:24:57
分享15 超级好用得 Python 实用技巧
2023-03-01 06:42:09
查看端口并杀进程python脚本代码
2022-06-26 21:58:32
Python处理字符串的常用函数实例总结
2022-10-19 21:09:39
![](https://img.aspxhome.com/file/2023/5/75175_0s.png)
BootStrap创建响应式导航条实例代码
2023-08-09 02:09:55
![](https://img.aspxhome.com/file/2023/4/56054_0s.gif)
利用Python编写简易的录制屏幕小工具
2023-12-17 04:34:57
![](https://img.aspxhome.com/file/2023/0/93690_0s.gif)
在go文件服务器加入http.StripPrefix的用途介绍
2023-06-26 07:19:07
![](https://img.aspxhome.com/file/2023/1/92031_0s.jpg)
python进程管理工具supervisor使用实例
2022-08-24 13:47:03
![](https://img.aspxhome.com/file/2023/8/75228_0s.png)
python代码实现学生信息管理系统
2023-02-06 18:31:34
![](https://img.aspxhome.com/file/2023/7/83917_0s.jpg)
Python常用知识点汇总
2023-02-17 09:42:44
![](https://img.aspxhome.com/file/2023/2/96992_0s.png)
Python高级架构模式知识点总结
2023-12-19 14:36:47
python中文分词,使用结巴分词对python进行分词(实例讲解)
2023-03-15 13:37:30
![](https://img.aspxhome.com/file/2023/8/95778_0s.png)
Python pandas如何向excel添加数据
2021-07-14 17:37:41
Python3 assert断言实现原理解析
2023-06-11 20:39:33
打分进化史
2009-12-24 12:20:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/taobao-84s.png)
Python中range、np.arange和np.linspace的区别
2023-10-17 14:45:14
![](https://img.aspxhome.com/file/2023/5/64265_0s.png)