TypeError: Object of type 'ndarray' is not JSON serializabl;TypeError: Object of type 'int64' is not_typeerror: object of type ndarray is not json seri

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

其中,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]
  • 1

4 . 返回json数据如下:

这里写图片描述