python实现class对象转换成json/字典的方法
作者:Ruthless 时间:2021-11-16 00:46:34
本文实例讲述了python实现class对象转换成json字典的方法。分享给大家供大家参考,具体如下:
# -*- encoding: UTF-8 -*-
class Student:
name = ''
age = 0
def __init__(self, name, age):
self.name = name
self.age = age
def convert_to_dict(obj):
'''把Object对象转换成Dict对象'''
dict = {}
dict.update(obj.__dict__)
return dict
def convert_to_dicts(objs):
'''把对象列表转换为字典列表'''
obj_arr = []
for o in objs:
#把Object对象转换成Dict对象
dict = {}
dict.update(o.__dict__)
obj_arr.append(dict)
return obj_arr
def class_to_dict(obj):
'''把对象(支持单个对象、list、set)转换成字典'''
is_list = obj.__class__ == [].__class__
is_set = obj.__class__ == set().__class__
if is_list or is_set:
obj_arr = []
for o in obj:
#把Object对象转换成Dict对象
dict = {}
dict.update(o.__dict__)
obj_arr.append(dict)
return obj_arr
else:
dict = {}
dict.update(obj.__dict__)
return dict
stu = Student('zhangsan', 20)
print '-----------'
print convert_to_dict(stu)
print '-----------'
print convert_to_dicts([stu, stu])
print '-----------'
print class_to_dict(stu)
print '-----------'
print class_to_dict([stu, stu])
stua = Student('zhangsan', 20)
stub = Student('lisi', 10)
stu_set = set()
stu_set.add(stua)
stu_set.add(stub)
print class_to_dict(stu_set)
运行结果如下:
-----------
{'age': 20, 'name': 'zhangsan'}
-----------
[{'age': 20, 'name': 'zhangsan'}, {'age': 20, 'name': 'zhangsan'}]
-----------
{'age': 20, 'name': 'zhangsan'}
-----------
[{'age': 20, 'name': 'zhangsan'}, {'age': 20, 'name': 'zhangsan'}]
[{'age': 10, 'name': 'lisi'}, {'age': 20, 'name': 'zhangsan'}]
希望本文所述对大家Python程序设计有所帮助。
标签:python,class,json,字典
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python Pandas 如何shuffle(打乱)数据
2023-04-22 23:17:16
![](https://img.aspxhome.com/file/2023/5/95855_0s.png)
Python读写Excel表格的方法
2022-11-10 20:43:18
![](https://img.aspxhome.com/file/2023/6/118556_0s.jpg)
在Python中操作列表之list.extend()方法的使用
2023-07-02 20:43:16
CSS Sprites对CSS布局的意义、优点和缺点介绍
2008-07-14 07:22:00
python_tkinter弹出对话框创建2
2023-10-17 06:27:29
![](https://img.aspxhome.com/file/2023/6/97106_0s.png)
Win8下python3.5.1安装教程
2021-11-15 13:14:54
![](https://img.aspxhome.com/file/2023/7/121877_0s.jpg)
Python3中exp()函数用法分析
2023-06-11 03:17:24
从事设计行业的十年
2008-04-01 09:44:00
Python多模块引用由此引发的相对路径混乱问题
2021-07-11 03:57:10
![](https://img.aspxhome.com/file/2023/8/124388_0s.png)
SQL的小常识, 备忘之用, 慢慢补充.
2011-11-03 17:19:45
Python读取配置文件-ConfigParser的二次封装方法
2023-06-15 09:19:58
![](https://img.aspxhome.com/file/2023/1/59651_0s.png)
python3实现无权最短路径的方法
2023-07-11 23:26:40
![](https://img.aspxhome.com/file/2023/2/76872_0s.png)
Python安装教程全过程(2022最新)
2022-05-09 06:10:55
![](https://img.aspxhome.com/file/2023/5/95215_0s.png)
python聊天室(虽然很简洁,但是可以用)
2021-05-21 01:10:46
![](https://img.aspxhome.com/file/2023/0/84120_0s.png)
Python 实现日志同时输出到屏幕和文件
2021-06-25 23:04:29
WEB2.0网页制作标准教程(4)如何调用css样式表
2007-11-13 13:26:00
Python查找数组中数值和下标相等的元素示例【二分查找】
2023-11-04 12:55:45
torch.optim优化算法理解之optim.Adam()解读
2023-06-24 23:30:25
![](https://img.aspxhome.com/file/2023/9/72739_0s.png)
500行Python代码打造刷脸考勤系统
2022-01-21 12:54:10
![](https://img.aspxhome.com/file/2023/7/118367_0s.jpg)
Pycharm 如何设置HTML文件自动补全代码或标签
2021-08-12 17:47:15
![](https://img.aspxhome.com/file/2023/3/121473_0s.png)