Python3 报错TypeError: Object of type 'bytearray' is not JSON serializable

在将返回结果转成json格式时遇到报错TypeError: Object of type 'bytearray' is not JSON serializable,打印了返回内容,显示返回结果如下:

返回内容

以下是网上前辈提供的资料。
《TypeError: Object of type 'bytes' is not JSON serializable》: https://blog.csdn.net/bear_sun/article/details/79397155;
《Python 学习之---内置数据结构---bytes & bytearray》: https://blog.csdn.net/qq_40498551/article/details/88948569

根据我这里的文件,返回内容有3种都是无法解析成json格式的数据的(bytearray/datetime/decimal),写了一个包含3种数据类型异常处理的解码类文件《MyEncoder.py》,然后再将这个文件导入到目标运行文件《connect_db.py》,并在文件中添加cls函数。如果返回内容中还有其他类型的数据转json异常,则在elif条件下添加转码方法即可。

#coding:utf-8
import  datetime
import json
import decimal

class MyEncoder(json.JSONEncoder):

    def default(self,obj):
        if isinstance(obj,bytearray):   #返回内容如果包含bytearray类型的数据
            return str(obj,encoding='utf-8')
        elif isinstance(obj,datetime.datetime): #返回内容如果包含datetime类型的数据
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj,decimal.Decimal):   #返回内容如果包含Decimal类型的数据
            return float(obj)
        super(DecimalEncoder, self).default(obj)

        return json.JSONEncoder.default(self,obj)
Python3 报错TypeError: Object of type 'bytearray' is not JSON serializable_第1张图片
添加cls函数

处理结果:


最终效果

你可能感兴趣的:(Python3 报错TypeError: Object of type 'bytearray' is not JSON serializable)