首页 > 其他分享 >TypeError: Object of type 'ndarray' is not JSON serializabl;TypeError: Object of type 

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

时间:2022-10-26 21:33:46浏览次数:45  
标签:count index TypeError res Object type


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]

4 . 返回json数据如下:

TypeError: Object of type


标签:count,index,TypeError,res,Object,type
From: https://blog.51cto.com/u_15847885/5798584

相关文章