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

將模型用flask封裝,返回json時報錯:TypeError: Object of type 'int32' is not JSON serializablejson

網上搜索出的解決方案:重寫json.JSONEncoderflask

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)
相關文章
相關標籤/搜索