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


將模型用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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM