python解决Object of type "xxx" is not JSON serializable

问题:Python json中的Object of type "xxx" is not JSON serializable通用解决办法

原因:返回的data数据格式不对

解决办法:

把向数据库请求到的数据转化为字典的键值对的格式 to_dict()

host_list = Host.query.filter().all()
if host_list:
    data = []
    for host in host_list:
        data.append(host.to_dict())
    return render_response(Retval.success, data)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下的资源也很不错, 加减可以看一下o 使用C++制作戰車射擊-100%提供源码 http://download.csdn.net/source/2257663 使用C++制作3D动画人物-100%提供源码 http://download.csdn.net/source/2255453 Linux kernel 每一行都完全注释-初学者必备 http://download.csdn.net/source/1982431 Programming Embedded Systems 2nd http://download.csdn.net/source/1982338 J2ME pdf and code 郭克华 http://download.csdn.net/source/2229809 C语言程序设计(Visual+C 6.0环境) http://download.csdn.net/source/2232878 Visual C++ 60 MFC + code 学习最强宝典 http://download.csdn.net/source/2236266 ASP.NET Web (第一次亲密接触ASP.NET) http://download.csdn.net/source/2241466 1 Introduction 1-1 11 Motivation 1-1 12 Objective of the Specification 1-2 13 Scope of the Document1-2 14 USB Product Compliance1-2 15 Document Organization1-3 16 Design Goals1-3 17 Related Documents1-3 2 Terms and Abbreviations 2-1 3 USB 30 Architectural Overview 3-1 31 USB 30 System Description 3-1 311 USB 30 Physical Interface3-2 3111 USB 30 Mechanical3-2 312 USB 30 Power 3-3 313 USB 30 System Configuration3-3 314 USB 30 Architecture Summary3-3 32 SuperSpeed Architecture 3-4 321 Physical Layer 3-5 322 Link Layer 3-6 323 Protocol Layer3-7 324 Robustness3-8 3241 Error Detection 3-8 3242 Error Handling 3-9 325 SuperSpeed Power Management3-9 326 Devices3-10 3261 Peripheral Devices 3-10 3262 Hubs3-11 327 Hosts 3-12 328 Data Flow Models3-12 4 SuperSpeed Data Flow Model 4-1 41 Implementer Viewpoints 4-1 42 SuperSpeed Communication Flow4-1 421 Pipes4-2 43 SuperSpeed Protocol Overview 4-2 431 Differences from USB 204-2 4311 Comparing USB 20 and SuperSpeed Transactions4-3 4312 Introduction to SuperSpeed Packets4-4 44 Generalized Transfer Description4-4 441 Data Bursting4-5 442 IN Transfers4-5 443 OUT Transfers4-6 444 Power Management and Performance4-7 Universal Serial Bus 30 Specification, Revision 10 viii 445 Control Transfers4-8 4451 Control Transfer Packet Size 4-8 4452 Control Transfer Bandwidth Requirements 4-8 4453 Control Transfer Data Sequences4-9 446 Bulk Transfers4-9 4461 Bulk Transfer Data Packet Size 4-9 446
这个错误通常发生在尝试将一个类型为"cleanmailresult"的Python对象转化为JSON格式时。它的原因可能是这个自定义对象没有实现JSON序列化的方法。 在Python中,JSON序列化是将一个对象转化为JSON格式的过程,以便可以在不同的系统之间进行数据传输和存储。然而,并不是所有的Python对象都可以直接被JSON序列化,只有那些内置的数据类型(如dict、list、str等)或者实现了JSON Encoder和Decoder方法的对象才可以被序列化。 解决这个问题的常见方法之一是实现一个自定义的JSON Encoder来处理这个对象的序列化。具体步骤包括: 1. 创建一个继承自`json.JSONEncoder`的自定义编码器类。 2. 在编码器类中,重写`default`方法,该方法会在遇到无法序列化的对象时被调用。 3. 在`default`方法中,判断对象的类型,如果是"cleanmailresult"类型的对象,则返回一个可以被JSON序列化的代表对象的字典。 4. 在使用`json.dumps()`函数时,指定`cls`参数为这个自定义的编码器类。 下面是一个示例代码: ```python import json class CleanMailResultEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, cleanmailresult): return obj.to_json() # 假设cleanmailresult对象有一个to_json()方法返回可序列化的字典 return super().default(obj) result = cleanmailresult() json_str = json.dumps(result, cls=CleanMailResultEncoder) ``` 通过实现一个自定义的JSON编码器类,我们可以将"cleanmailresult"对象转化为JSON格式。请注意,这里的示例代码中的`to_json()`方法是一个假设的方法,你需要根据实际情况来实现这个方法返回可序列化的字典。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值