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

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/nima1994/article/details/81605651

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 jsonify(res)

其中,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数据如下:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/nima1994/article/details/81605651