json存储格式问题

1
2
3
4
5
6
7
8
9
10
11
12
13
问题:TypeError: Object of type 'int64' is not JSON serializable (或者float32)

class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
elif isinstance(obj, np.floating):
return float(obj)
elif isinstance(obj, np.ndarray):
return obj.tolist()
else:
return super(NpEncoder, self).default(obj)
json.dumps(cxy_data, ensure_ascii=False,cls=NpEncoder)