记一个JSON返回数据的bug

记一个JSON返回数据的bug:'Object of type int64 is not JSON serializable'

我在打包数组进行json数据返回时,有一个参数是numpy数组里计算出来的,类型为int64,直接进行json打包会报错

提示('Object of type int64 is not JSON serializable',)

,查阅了一些资料,这是因为 JSON 标准规定只支持有限的数据类型,而 int64 不是其中之一。

然后我又搜了一下,python的数据类型,int64和int有什么区别,int64在 什么情况下会出现,得到如下答案:

在 Python 中,int 是整数的基本数据类型,而 int64 是 numpy 库中的数据类型,用于表示 64 位整数。

int 是 Python 内置的整数类型,它可以表示任意大小的整数。在大多数情况下,我们使用的整数都是 int 类型。

而 int64 是 numpy 库中的数据类型,用于处理需要精确表示 64 位整数的情况。int64 类型的整数范围更大,可以表示更大的整数值,而不会溢出。

通常情况下,我们不会直接使用 int64 类型,除非需要处理大整数或者需要与 numpy 库进行交互时。在一些科学计算和数据分析的场景中,numpy 库常常会使用 int64 类型来表示整数数据。

需要注意的是,Python 的内置整数类型 int 在大多数情况下可以满足我们的需求,而且更常见和易于使用。只有在特定的情况下,比如需要处理大整数或者与 numpy 库进行交互时,才需要使用 int64 类型。

后来我把那段代码进行格式转化,转换成float类型,才没有报这个错误了

下面附上代码片段:

python 复制代码
                    for dimension in dimension_list:
                        element_df = result.loc[result['dimension'] == dimension].reset_index(drop=True)
                        dimension_dict = {}
                        dimension_dict['dimension'] = dimension
                        dimension_dict['JS'] = element_df['js_sum'].loc[0]
                        dimension_dict['EP'] = element_df['ep_sum'].loc[0]
                        dimension_dict['element'] = element_df[['elements', 'surprise', 'EP']].reset_index(drop=True).to_dict(orient='records')
                        result_list.append(dimension_dict)
                    json_str["rcode"] = 0
                    json_str["desc"] = "定位根因success"
                    json_str["dimension"] = result_list

修改后的:

python 复制代码
                    for dimension in dimension_list:
                        element_df = result.loc[result['dimension'] == dimension].reset_index(drop=True)
                        dimension_dict = {}
                        dimension_dict['dimension'] = dimension
                        dimension_dict['JS'] = float(element_df['js_sum'].loc[0])
                        dimension_dict['EP'] = float(element_df['ep_sum'].loc[0])
                        dimension_dict['element'] = element_df[['elements', 'surprise', 'EP']].reset_index(drop=True).to_dict(orient='records')
                        result_list.append(dimension_dict)
                    json_str["rcode"] = 0
                    json_str["desc"] = "定位根因success"
                    json_str["dimension"] = result_list
相关推荐
weixin-a153003083165 分钟前
[数据抓取-1]beautifulsoup
开发语言·python·beautifulsoup
AI量化投资实验室10 分钟前
15年122倍,年化43.58%,回撤才20%,Optuna机器学习多目标调参backtrader,附python代码
人工智能·python·机器学习
倔强青铜三21 分钟前
苦练Python第67天:光速读取任意行,linecache模块解锁文件处理新姿势
人工智能·python·面试
我是华为OD~HR~栗栗呀1 小时前
华为od-21届考研-C++面经
java·c语言·c++·python·华为od·华为·面试
明月(Alioo)1 小时前
机器学习入门,无监督学习之K-Means聚类算法完全指南:面向Java开发者的Python实现详解
python·算法·机器学习
鱼鱼说测试1 小时前
Linux下运行Jmeter
开发语言·python
CodeCraft Studio2 小时前
国产化Excel开发组件Spire.XLS教程:将Python列表转换为Excel表格(3种实用场景)
开发语言·python·excel·spire.xls·python列表转excel·国产化文档开发
企鹅侠客2 小时前
基于python写的PDF表格提取到excel文档
python·pdf·excel·pdf文档表格转excel
mortimer3 小时前
Python 中那些鲜为人知但实用的工具函数
python
weixin_421133413 小时前
Django 的文档接口
python·django·sqlite