当前位置:网站首页>解决 json.dump 报错:TypeError - Object of type xxx is not JSON serializable
解决 json.dump 报错:TypeError - Object of type xxx is not JSON serializable
2022-08-05 11:34:00 【为为为什么】
在python中导入json包可以方便地操作json文件,但是偶尔会遇到 TypeError: Object of type xxx is not JSON serializable 错误,通常报错的位置是很正常的int或float,本文记录该问题解决方法。
自定义序列化方法
class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, time):
return obj.__str__()
else:
return super(MyEncoder, self).default(obj)调用json包写入数据时加入
json.dump(final_json, fp, indent=3, cls= MyEncoder)边栏推荐
- #yyds干货盘点#【愚公系列】2022年08月 Go教学课程 001-Go语言前提简介
- sqlserver编写通用脚本实现获取一年前日期的方法
- hdu4545 Magic String
- TiDB 6.0 Placement Rules In SQL 使用实践
- 知乎提问:中国是否还能实现伟大民族复兴
- 硅谷来信:快速行动,Facebook、Quora等成功的“神器”!
- Custom filters and interceptors implement ThreadLocal thread closure
- .NET in-depth analysis of the LINQ framework (6: LINQ execution expressions)
- The fuse: OAuth 2.0 four authorized login methods must read
- LeetCode刷题(8)
猜你喜欢
随机推荐
“蘑菇书”是怎样磨出来的?
UDP通信
2022 CCF国际AIOps挑战赛决赛暨AIOps研讨会报名已开启
nyoj757 期末考试 (优先队列)
CenOS MySQL入门及安装
Android development with Kotlin programming language three loop control
平安萌娃卡保险怎么样?让父母读懂几个识别产品的方法
【硬件架构的艺术】学习笔记(2)同步和复位
【心里效应】98 个著名的心理效应
巴比特 | 元宇宙每日必读:中国1775万件数字藏品分析报告显示,85%的已发行数藏开通了转赠功能...
5G NR system messages
版本控制篇 | 龙智邀您共赴GOPS全球运维大会,探索大规模、敏捷、高质量、开放式的软件研发与运营之路
PPOCR 检测器配置文件参数详解
【AGC】增长服务1-远程配置示例
Go编译原理系列9(函数内联)
字节秋招二面把我干懵了,问我SYN报文什么情况下会被丢弃?
Support Vector Machine SVM
power failure...Trouble trouble trouble!!!
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
大佬们 我是新手,我根据文档用flinksql 写个简单的用户访问量的count 但是执行一次就结束









