Object of type ndarray is not JSON serializable

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)
添加上面的代码

使用方法 

 with open('ec_dict1.json', 'w')as f:
     json.dump(data, f, ensure_ascii=False, cls=NpEncoder)

 

posted on 2021-06-07 09:54  闹不机米  阅读(618)  评论(0编辑  收藏  举报

导航