MySQL Decimal is not JSON serializable以及插入小数变成0
2021-03-01 22:19
911 查看
使用Python搭建的web服务,后台读取MySQL数据后,需要将数据序列化为json串,返回给前端。但是如果MySQL的字段是decimal类型,序列化为json串就会遇到麻烦。会报如下错误
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Decimal('0') is not JSON serializable
HTTP/1.0" 500
网上有一些解决方案,但是如果你对于数据精度的要求没那么高的话,完全可以把MySQL中的decimal字段的类型改为float,float类型是可以直接进行json序列化的。这样只修改数据库,不修改代码,就可以修复问题。参考下图。另外,设置float类型的时候,小数点后一定要设置,可以设置为4,表示带4位小数。否则默认可能是带0位小数,就不准确了。如果你insert的数据类似‘0.022’这种,在数据库中就变成0了。
建议使用Navicat for MySQL来操作数据库,这款软件是收费的,很容易破解,当然还是鼓励大家使用正版。鼠标右击表的名称,选择“设计表”,然后你会看到上面的图片内容,里面可以修改字段类型,长度,增加索引等,比语句操作方便多了。
相关文章推荐
- python-TypeError: Object of type 'Decimal' is not JSON serializable 报错
- Object of type Decimal is not JSON serializable(decimal类型的对象不可JSON序列化)
- TypeError: datetime.datetime(2016, 9, 25, 21, 12, 19, 135649) is not JSON serializable解决办法(json无法序列化对象的解决办法)
- TypeError: Object of type 'int32' is not JSON serializable
- TypeError: datetime is not JSON serializable
- mysql插入小数变成整数的解决方法
- 在使用json.dumps时遇到报错TypeError: Object of type 'float32' is not JSON serializable
- mysql配置文件不生效以及配置同步复制报错“The server is not configured as slave”解决办法
- datetimeTypeError: datetime.datetime(2015, 1, 29, 8, 17, 41) is not JSON serializable
- mysql配置文件不生效以及配置同步复制报错“The server is not configured as slave”解决办法...
- Python3运行报错TypeError: Object of type bytes is not JSON serializable
- 问题解决:json.dump使用时报错int64 is not jason serializable
- storm启动问题解析Storm conf is not valid. Must be json-serializable
- 转json报错:TypeError: Object of type int64 is not JSON serializable
- json.dump throwing “TypeError: {…} is not JSON serializable”
- python mysql 插入数据报错 Failed processing format-parameters; 'int' object is not iterable
- Python3 flask 解析post的json数据时报错,TypeError: Object of type 'bytes' is not JSON serializable
- 使用numpy库报错,TypeError: Object of type 'int64' is not JSON serializable.
- datetime.datetime() is not JSON serializable
- Django自定义数据段问题即 [] is not JSON serializable(数组非JSON序列化)