前言

使用 json.dumps(result) 对数据转 JSON 数据出现错误:TypeError: Object of type float32 is not JSON serializable

数据中存在的 float32 数据是 numpy 格式的数据,Python 内置的 float 类型可以写入 JSON 中,但是 numpy 的 float32 类型数据不能写入 JSON,所以应将 numpy.float32 类型数据转成 Python.float 类型数据

解决

在函数中使用 str() 函数将 result 转成字符串

print("result", result)
dumps = json.dumps(str(result))
print("dumps", dumps)

这样代码确实可以运行,但是转换的结果却是整个字符串

除此之外,见到有博友封装,如下:

import numpy as np

# 对numpy的数据类型进行转换
# 场景:numpy.float32类型不能写入JSON,需要转成Python的float类型
def convertNumpyDataType(data):
if type(data) is list:
return convertList(data)
elif type(data) is dict:
return convertDict(data)
elif type(data) is np.float32:
return convertFloat32(data)
return data def convertList(data):
if type(data) is not list:
return data temp = []
for obj in data:
temp.append(convertNumpyDataType(obj)) return temp def convertDict(data):
temp = data.copy()
if type(data) is not dict:
return temp for key in data.keys():
obj = data.get(key)
temp.__setitem__(key, convertNumpyDataType(obj)) return temp def convertFloat32(data):
return float(data)

Python Object of type float32 is not JSON serializable的更多相关文章

  1. celery 4.1下报kombu.exceptions.EncodeError: Object of type 'bytes' is not JSON serializable 处理方式

    #python代码如下 from celery import Celeryimport subprocess app = Celery('tasks', broker='redis://localho ...

  2. Object of type 'ListSerializer' is not JSON serializable “listserializer”类型的对象不可JSON序列化

    Object of type 'ListSerializer' is not JSON serializable “listserializer”类型的对象不可JSON序列化 一般原因为 序列化的对象 ...

  3. TypeError: Object of type 'int64' is not JSON serializable

    错误类型:TypeError: Object of type 'int64' is not JSON serializable 错误场景:对Numpy和Pandas结果进行json.dumps报错 错 ...

  4. TypeError: Object of type 'int32' is not JSON serializable ——已解决

    将模型用flask封装,返回json时报错:TypeError: Object of type 'int32' is not JSON serializable 网上搜索出的解决方案:重写json.J ...

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

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

  6. Object of type 'ndarray' is not JSON serializable

    Object of type 'ndarray' is not JSON serializable import numpy as np import json arr=np.asarray([345 ...

  7. TypeError: Object of type 'int32' is not JSON serializable

    将模型用flask封装,返回json时报错:TypeError: Object of type 'int32' is not JSON serializable 网上搜索出的解决方案:重写json.J ...

  8. TypeError: Object of type 'ListSerializer' is not JSON serializable

    问题: 解决:ser.data是json数据,你想要的

  9. Object of type Decimal is not JSON serializable

    json遇到Decimal 型数据无法正确处理 解决方案 import json result = [ {'name': '小红', 'age': 26, 'balance': decimal.Dec ...

  10. typeerror object of type ‘decimal‘ is not json serializable jsonify

    当使用flask的jsonify返回json数据时,由于数据库有些字段类型使用decimal,而jsonify无法处理 解决方案 导入下面的包即可解决 pip install simplejson

随机推荐

  1. Qt编写地图综合应用8-地图交互

    一.前言 最常用的地图交互就几个,比如鼠标在地图上按下的时候可以拾取经纬度坐标,然后传给Qt程序,再比如对设置的设备点进行单击的时候,通知Qt程序单击了哪一个设备点,好让Qt程序识别并作出反应比如弹出 ...

  2. JMeter使用指南+实验报告

    JMeter使用指南 目录 JMeter使用指南 界面基本配置方法 1.选项里的放大与缩小--缩放字体 2.选项里的选择语言 3.命令行的调出 注意事项 一些指标介绍 1.TCP取样器 2.汇总/聚合 ...

  3. pitch、yaw、roll三个角的区别

    Z轴正方向为前进方向 pitch():俯仰,将物体绕X轴旋转(localRotationX) yaw():航向,将物体绕Y轴旋转(localRotationY) roll():横滚,将物体绕Z轴旋转( ...

  4. Web网页端IM产品RainbowChat-Web的v6.0版已发布

    一.关于RainbowChat-Web RainbowChat-Web是一套Web网页端IM系统,是RainbowChat的姊妹系统(RainbowChat是一套基于开源IM聊天框架 MobileIM ...

  5. 不为人知的网络编程(十四):拔掉网线再插上,TCP连接还在吗?一文即懂!

    本文由作者小林coding分享,来自公号"小林coding",有修订和改动. 1.引言 说到TCP协议,对于从事即时通讯/IM这方面应用的开发者们来说,再熟悉不过了.随着对TCP理 ...

  6. JMeter JDBC 请求实战宝典

    <JMeter JDBC 请求实战宝典> 宝子们,今天咱就来唠唠 JMeter 里超厉害的 JDBC 请求,这玩意儿就像是数据库世界的神奇魔杖,能帮咱把数据库里的各种秘密(数据)都挖出来, ...

  7. Solution Set - “请背诵每条魔法的禁忌”

    目录 0.「HAOI 2018」「洛谷 P4494」反色游戏 1.「JSOI 2010」「洛谷 P6029」旅行 2.「CTSC 2017」「洛谷 P3774」最长上升子序列 3.「CTSC 2018 ...

  8. CDS标准视图:维护项目数据 C_MaintenanceItemDEX

    视图名称:维护项目数据 C_MaintenanceItemDEX 视图类型:基础 视图代码: 点击查看代码 @AbapCatalog.sqlViewName: 'CMAINTITEMDEX' @Aba ...

  9. 阿里云-对象存储OSS

    https://help.aliyun.com/product/31815.html 阿里云对象存储OSS(Object Storage Service)是一款海量.安全.低成本.高可靠的云存储服务, ...

  10. nginx.conf参数优化详解

    1.Niginx主配置文件参数详解 a.上面博客说了在Linux中安装nginx.博文地址为:http://www.cnblogs.com/hanyinglong/p/5102141.html b.当 ...