rect_list = list()...rect_list.append(rect1)rect_list.append(rect2)...rsp = {'rect-list': rect_list}return json.dumps(rsp) 

报错

  File "C:\Users\mo\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 257, in iterencodereturn _iterencode(o, 0)File "C:\Users\mo\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 179, in defaultraise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type int32 is not JSON serializable

原因

字典格式化为JSON不支持numpy.int32。np.float32、np.array等存在类似问题。

解决(两种 方法原理相同)

  • 转Json前,将np类型强制转换为python类型
def cv_rect_to_dict(cv_rect):x, y, w, h = cv_rectreturn {'x': int(x), 'y': int(y), 'w': int(w), 'h': int(h)}
  • 继承实现自定义Json Encoder
class ZJsonEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, numpy.integer):return int(obj)elif isinstance(obj, numpy.floating):return float(obj)elif isinstance(obj, numpy.ndarray):return obj.tolist()else:return super(ZJsonEncoder, self).default(obj)用法:rsp = {'rect-list': rect_list}return json.dumps(rsp, cls=ZJsonEncoder)

记录:TypeError: Object of type int32 is not JSON serializable。相关推荐

  1. 成功解决TypeError: Object of type 'ndarray' is not JSON serializable

    解决问题 TypeError: Object of type 'ndarray' is not JSON serializable 解决方法 def default(self, obj):if isi ...

  2. labelme2coco问题:TypeError: Object of type 'int64' is not JSON serializable

    最近在做MaskRCNN 在自己的数据(labelme)转为COCOjson格式遇到问题:TypeError: Object of type 'int64' is not JSON serializa ...

  3. Flask API TypeError: Object of type 'Response' is not JSON serializable

    Flask API TypeError: Object of type 'Response' is not JSON serializable 错误代码: session['image'] = str ...

  4. 【文件处理】——字典写入json文件或TXT文件,读取文件中的字典TypeError: Object of type ‘ndarray‘ is not JSON serializable错误解决方法

    目录 一.将字典写入json文件 二.json文件中读取字典 三.将字典写入TXT文件中 四.从TXT中读取字典 五.解决字典含数组存入json文件失败的方法 1.存入前将数组变成列表 2.扩展类方法 ...

  5. TypeError: Object of type 'datetime' is not JSON serializable

    json序列化时间对象的时候报错: TypeError: Object of type 'datetime' is not JSON serializable 解决办法 重写json序列化类 # -* ...

  6. TypeError: Object of type set is not JSON serializable

    今天运行flask项目突然报TypeError: Object of type set is not JSON serializable错误,上网搜了一下 该对象是set形式,json序列不支持,回到 ...

  7. 返回 JSON 格式数据报错:TypeError: Object of type set is not JSON serializable

    在做 flask 项目的时候需要返回一个 JSON 数据,运行的过程中却报错:TypeError: Object of type set is not JSON serializable 报错位置如下 ...

  8. Python TypeError: Object of type ‘Decimal‘ is not JSON serializable 类型错误 无法json

    场景:今天使用python 查询了一个MYSQL 数据库的信息  数据库的字段为decimal 类型 我将结果进行json.dumps 报错 TypeError: Object of type 'De ...

  9. TypeError: Object of type ‘TrackedArray‘ is not JSON serializable

    如果x是Object of type 'TrackedArray',那么这里使用x.tolist()可以解决问题,转化为list

最新文章

  1. 趣谈iOS运行时的方法调用原理
  2. Day 8 Linux 优化-补充目录结构
  3. 操作主机 Infrastructure Master[为企业维护windows server 2008系列八]
  4. Spring AOP 实战运用
  5. P2424 约数和 真丶除法分块
  6. iOS开发之NSURLSession/NSURLConnection HTTP load failed 的解决办法
  7. 三菱a系列motion软体_三菱PLC全系列编程电缆制作方法
  8. 计算机ascii码表
  9. [论文导读]Restoring and attributing ancient texts using deep neural networks深度学习复原古希腊铭文
  10. 如何进bios设置ssd固态硬盘为第一启动
  11. 订单除了快递、达达同城以外,可设置到店自取
  12. fttp项目下载和上传
  13. Lumaqq移植到Android 之进阶篇
  14. 大型数据中心互联(T级光传输方案)
  15. https协议和Htt协议
  16. CVPR 2018视频行为识别挑战赛概览
  17. 算法学习之路|幼儿园买玩具
  18. 常见的爬虫乱码的解决办法
  19. 单片机17种常见的电路设计模块
  20. STM32串口通信控制pwm

热门文章

  1. 人们为什么要在微博、Facebook 等上发布自己的状态信息或者人生感悟?
  2. 【python画图】使用python画折线图、柱状图
  3. 大数据的属性是什么?如何划分?
  4. 米联客 CH02基于ZYNQ的嵌入式LINUX移植
  5. SQL Server Error: Saving Changes is not permitted. The changes you have made require the following t
  6. 30岁软件工程师的迷茫和悲哀!
  7. iOS扫码一图多码原生处理AVCaptureSession
  8. 最新CSM会议室预约系统源码+功能强大
  9. 动态修改el-input样式;动态修改elmentUI元素样式;css变量
  10. An Introduction to ANCOVA (Analysis of Variance) 协方差分析 看某个treatment排除其他因素后对结果是否有显著性影响