前言

使用 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. mysql数据库 主从同步

    我们知道mysql数据库为了得到更高性能,一般会读写分离.主库用于写操作,比如用于执行的insert.update操作:从库用于读,也就是常见的select操作. 写数据都在主库(master)操作, ...

  2. 关于Qt高分屏缩放几个知识点

    在windows上经常遇到高分屏缩放的问题,很头疼,貌似这东西就是windows首发的. 在Qt4时代的程序遇到高分屏缩放,不作任何处理,毕竟Qt4时代(2010年以前)出来的时候几乎还没高分屏缩放这 ...

  3. [转]OpenCV4.8 GPU版本CMake编译详细步骤 与CUDA代码演示

    导 读 本文将详细介绍如何使用CMake编译OpenCV4.8 CUDA版本并给出Demo演示,方便大家学习使用. CMake编译详细步骤 废话不多说,直接进入正题! [1]我使用的工具版本VS201 ...

  4. [转]CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options

    这个错误是使用webpack的一个plugin出现的错误.这个plugin是copy-webpack-plugin我把文档的例子复制,然后就报了这个错误.文档的例子: const CopyPlugin ...

  5. [.NET] 单位转换实践:深入解析 Units.NET

    单位转换实践:深入解析 Units.NET 摘要 在现代软件开发中,准确处理不同单位的转换是一个常见而复杂的需求.无论是处理温度.长度.重量还是其他物理量,都需要可靠的单位转换机制.本文将深入介绍 U ...

  6. 【漏洞分析】20250105-SorraStaking:奖励金额计算错误,每次取款都有大收益

    背景信息 2024-12-21 11:58:11 (UTC) 准备交易:https://app.blocksec.com/explorer/tx/eth/0x72a252277e30ea6a37d2d ...

  7. Java工具类HttpClientUtil

    1. 依赖包 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId> ...

  8. 分布式配置中心--Apollo

    分布式配置中心--Apollo Apollo(阿波罗)是携程开源的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,支持配置热发布并实时推送到应用端,并且具备规范的权限及流程治理等特性,适用 ...

  9. ISA-L库调研

    本文分享自天翼云开发者社区<ISA-L库调研>,作者:何****尔 1.Intel SIMD指令集 SIMD(single instruction multiple data)单指令多数据 ...

  10. idea遇见Command line is too long. Shorten command line for Main or also for Application default configuration?

    <property name="dynamic.classpath" value="true" /> 第一步:找到项目目录下的.idea\works ...