Object of type Decimal is not JSON serializable
json遇到Decimal 型数据无法正确处理
解决方案
import json
result = [
{'name': '小红', 'age': 26, 'balance': decimal.Decimal(21.56)},
{'name': '小明', 'age': 24, 'balance': decimal.Decimal(31.23)},
]
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
super(DecimalEncoder, self).default(o)
# jsonData是结合上下文自己定义的
# ensure_ascii=False,显示中文
result = json.dumps(result, cls=DecimalEncoder, ensure_ascii=False)
print(result)
Object of type Decimal is not JSON serializable的更多相关文章
- typeerror object of type ‘decimal‘ is not json serializable jsonify
当使用flask的jsonify返回json数据时,由于数据库有些字段类型使用decimal,而jsonify无法处理 解决方案 导入下面的包即可解决 pip install simplejson
- 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 ...
- Object of type 'ListSerializer' is not JSON serializable “listserializer”类型的对象不可JSON序列化
Object of type 'ListSerializer' is not JSON serializable “listserializer”类型的对象不可JSON序列化 一般原因为 序列化的对象 ...
- TypeError: Object of type 'int64' is not JSON serializable
错误类型:TypeError: Object of type 'int64' is not JSON serializable 错误场景:对Numpy和Pandas结果进行json.dumps报错 错 ...
- TypeError: Object of type 'int32' is not JSON serializable ——已解决
将模型用flask封装,返回json时报错:TypeError: Object of type 'int32' is not JSON serializable 网上搜索出的解决方案:重写json.J ...
- labelme2coco问题:TypeError: Object of type 'int64' is not JSON serializable
最近在做MaskRCNN 在自己的数据(labelme)转为COCOjson格式遇到问题:TypeError: Object of type 'int64' is not JSON serializa ...
- 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 ...
- TypeError: Object of type 'int32' is not JSON serializable
将模型用flask封装,返回json时报错:TypeError: Object of type 'int32' is not JSON serializable 网上搜索出的解决方案:重写json.J ...
- TypeError: Object of type 'ListSerializer' is not JSON serializable
问题: 解决:ser.data是json数据,你想要的
随机推荐
- layui动态添加选项卡
<!DOCTYPE html><html xmlns:th="http://www.thymeleaf.org"><head> <meta ...
- spring给容器注册组件 的几种方式
环境搭建: 新建一个maven项目,引入依赖 <dependency> <groupId>org.springframework</groupId> <art ...
- JVM内存模型不再是秘密
前言 上篇文章我们一起了解了jvm虚拟机类的加载机制,而且是以一种纯大白话进行的一场闲聊,相信小伙伴们应该印象深刻,感兴趣的小伙伴可以重温一下上一篇文章大白话谈JVM的类加载机制. 当jvm加载了类后 ...
- LVM的简单使用及常用的命令总结
Lvm的简单使用及常用的命令总结 centos7中默认使用的是xfs文件系统,此文件系统的特点,可以另外查找资料,这里说一下对文件系统的扩容: 1.先看一下没扩容之前的分区大小 2.添加一块新磁盘,并 ...
- idea报“Cannot resolve symbol XXX”错误
解决方案
- IDEA设置External Tools之Javap反编译字节码
通过Jdk的命令javap可以反编译查看字节码,但是在使用idea的时候一直用命令行去操作不太好操作,而且因为idea会把class码 放在target里面,经常会忘记切换目录.这个时候idea的Ex ...
- VBScript 教程
VBScript 教程 VB 不区分大小写 变量 普通变量 关键词声明 Dim.Public.Private 赋值动态创建 name = "hello" Option Explic ...
- CSS的背景
CSS的背景 1. 背景颜色background-color div { background-color: 颜色值; } 一般情况下元素背景颜色默认是transparent(透明). 2. 背景图片 ...
- 【7】进大厂必须掌握的面试题-Java面试-Jsp
1. jsp的生命周期方法是什么? 方法 描述 公共无效的jspInit() 与servlet的init方法相同,仅被调用一次. 公共无效_jspService(ServletRequest requ ...
- swoole执行外部程序称为进程
<?php $child = new \Swoole\Process(function(\Swoole\Process $process){ $process->exec('/usr/lo ...