TypeError: Object of type ‘ndarray‘ is not JSON serializable_object of type ndarray is not json serializable

json不认numpy的array

import numpy as np
import json
a=np.asarray([1,2])
result={'name':'test','num':a}
json.dump(result)
  • 1
  • 2
  • 3
  • 4
  • 5

TypeError: Object of type ‘ndarray’ is not JSON serializable

解决方案:

result={'name':'text','num':a.tolist()}
  • 1