Object of type 'ndarray' is not JSON serializable

Object of type 'ndarray' is not JSON serializable

import numpy as np
import json


ar=np.asarray([345,45])
result={'asdf':'asdf','54':ar}
with open("./hmm.json", 'w', encoding='utf-8') as json_file:
    json.dump(result,json_file,ensure_ascii=False)

解决方法:

result={'asdf':'asdf','54':ar.tolist()}

json不认numpy的array。

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/81461192
今日推荐