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

2023-05-19,,

将模型用flask封装,返回json时报错:TypeError: Object of type 'int32' is not JSON serializable

网上搜索出的解决方案:重写json.JSONEncoder

class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, numpy.integer):
return int(obj)
elif isinstance(obj, numpy.floating):
return float(obj)
elif isinstance(obj, numpy.ndarray):
return obj.tolist()
else:
return super(MyEncoder, self).default(obj)
json.dumps(data,cls=MyEncoder)

参考博客:

https://www.cnblogs.com/lyq-bk1/p/9597172.html

TypeError: Object of type 'int32' is not JSON serializable的相关教程结束。

《TypeError: Object of type 'int32' is not JSON serializable.doc》

下载本文的Word格式文档,以方便收藏与打印。