天天看点

成功解决TypeError: Object of type 'ndarray' is not JSON serializable

解决问题

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

解决方法

def default(self, obj):

   if isinstance(obj, (numpy.int_, numpy.intc, numpy.intp, numpy.int8,

       numpy.int16, numpy.int32, numpy.int64, numpy.uint8,

       numpy.uint16,numpy.uint32, numpy.uint64)):

       return int(obj)

   elif isinstance(obj, (numpy.float_, numpy.float16, numpy.float32,

       numpy.float64)):

       return float(obj)

   elif isinstance(obj, (numpy.ndarray,)): # add this line

       return obj.tolist() # add this line

   return json.JSONEncoder.default(self, obj)

成功解决!