首页 > Web开发 > 详细

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

时间:2018-09-06 11:55:42      阅读:3754      评论:0      收藏:0      [点我收藏+]

将模型用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)

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!