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的更多相关文章

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

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

  2. 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 ...

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

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

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

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

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

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

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

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

  7. 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 ...

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

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

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

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

随机推荐

  1. 细嚼JS闭包知识点及案例分析

    闭包是js开发惯用的技巧,什么是闭包? 闭包指的是:能够访问另一个函数作用域的变量的函数.清晰的讲:闭包就是一个函数,这个函数能够访问其他函数的作用域中的变量.默认闭包的this指向windows. ...

  2. OpenCV图像处理学习笔记-Day4(完结)

    OpenCV图像处理学习笔记-Day4(完结) 第41课:使用OpenCV统计直方图 第42课:绘制OpenCV统计直方图 pass 第43课:使用掩膜的直方图 第44课:掩膜原理及演示 第45课:直 ...

  3. 怎么摆脱又臭又长的 Git 命令?

    在使用 Git 的时候,虽然大部分时候我们用的是图形客户端,但图形客户端不能解决所有的操作,所以,也少不了要需要使用命令行的时候. 但有些 Git 命令和参数确实又不好记,甚至有的命令每次要用的时候需 ...

  4. 联赛模拟测试8 Dash Speed 线段树分治

    题目描述 分析 对于测试点\(1\).\(2\),直接搜索即可 对于测试点\(3 \sim 6\),树退化成一条链,我们可以将其看成序列上的染色问题,用线段树维护颜色相同的最长序列 对于测试点\(7\ ...

  5. C语言中time_t数据类型详细介绍

    包含文件:<time.h> #ifndef __TIME_T #define __TIME_T     /* 避免重复定义 time_t */ typedef long     time_ ...

  6. MySQL数据库的完全备份与恢复

    前言 在生产环境中,数据的安全性是至关重要的,任何数据的丢失都可能产生严重的后果,正确的数据库操作是实际环境下不可缺少的. 一.数据库备份的分类 1.1 从物理与逻辑的角度,备份的分类 物理备份:对数 ...

  7. Springboot+Redis(发布订阅模式)跨多服务器实战

    一:redis中发布订阅功能(http://www.redis.cn/commands.html#pubsub) PSUBSCRIBE pattern [pattern -]:订阅一个或者多个符合pa ...

  8. 原生js实现一个自定义下拉单选选择框

    浏览器自带的原生下拉框不太美观,而且各个浏览器表现也不一致,UI一般给的下拉框也是和原生的下拉框差别比较大的,这就需要自己写一个基本功能的下拉菜单/下拉选择框了.最近,把项目中用到的下拉框组件重新封装 ...

  9. 并发压测 jmeter使用教程

    百度网盘下载软件 提取码: 2nur 第一步:首先从jmeter的官网下载jmeter,目前最新版本为4.0,支持的JDK最高为1.8 下载地址: jmeter:http://jmeter.apach ...

  10. spring boot:用dynamic-datasource-spring-boot-starter配置多数据源访问seata(seata 1.3.0 / spring boot 2.3.3)

    一,dynamic-datasource-spring-boot-starter的优势? 1,dynamic-datasource-spring-boot-starter 是一个基于springboo ...