天天看點

TypeError: Object of type 'ndarray' is not JSON serializabl;TypeError: Object of type 'int64' is not

1 . python使用​

​json.jsonify​

​​ 将結果轉為json格式時,爆出如上​

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

​​錯誤。

(flask)代碼如下:

@app.route('/predict/counts')
def predcitcounts():
    index, count = data_analyse.time_counts()
    res={}
    res['index']=index
    res['count']=count ##1
    return      

其中,index,count類型分别為:​

​<class 'list'> <class 'numpy.ndarray'>​

​。

2 . 将​

​##1​

​​處代碼改為​

​res['count']=list(count)​

​​,則報錯 “​

​TypeError: Object of type 'int64' is not JSON serializable​

​”。

3 . 故​

​##1​

​修改為:

res['count']=[str(i) for i in count]