当前位置:网站首页>解决 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)边栏推荐
猜你喜欢
随机推荐
有多一只“手”的机器狗出没?就在昇腾AI开发者创享日·南京站
.NET深入解析LINQ框架(六:LINQ执行表达式)
Image segmentation model - a combination of segmentation_models_pytorch and albumations to achieve multi-category segmentation
5G NR system messages
MySQL 中 auto_increment 自动插入主键值
Oracle的自动段空间管理怎么关闭?
可视化开发必看:智慧城市四大核心技术
巴比特 | 元宇宙每日必读:中国1775万件数字藏品分析报告显示,85%的已发行数藏开通了转赠功能...
Naive bayes
数据治理体系演进简介
hdu 1870 愚人节的礼物 (栈)
Letter from Silicon Valley: Act fast, Facebook, Quora and other successful "artifacts"!
Go学习笔记(篇二)初识Go
Machine Learning - Ensemble Learning
Gray value and thermal imaging understanding
脱光衣服待着就能减肥,当真有这好事?
【AGC】增长服务1-远程配置示例
TiDB 6.0 Placement Rules In SQL 使用实践
SkiaSharp 之 WPF 自绘 投篮小游戏(案例版)
Web3 中的安全问题和防范









