python - 在 Python 中序列化 JSON 时出现 "TypeError: (Integer) is not JSON serializable"?

标签 python json encoding typeerror

我正在尝试从 python 向 json 文件发送一个简单的字典,但我不断收到“TypeError: 1425 is not JSON serializable”消息。

import json
alerts = {'upper':[1425],'lower':[576],'level':[2],'datetime':['2012-08-08 15:30']}
afile = open('test.json','w')
afile.write(json.dumps(alerts,encoding='UTF-8'))
afile.close()

如果我添加默认参数,那么它会写入,但整数值会作为字符串写入 json 文件,这是不可取的。

afile.write(json.dumps(alerts,encoding='UTF-8',default=str))

最佳答案

我发现了我的问题。问题是我的整数实际上是类型 numpy.int64 .

关于python - 在 Python 中序列化 JSON 时出现 "TypeError: (Integer) is not JSON serializable"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11942364/

相关文章:

javascript - 如何使用 jquery 和 ajax 更新 json 中的数据

bash - Sed 和 UTF-8 编码

c# - C# double 的尾数归一化

python - "from a import b"不起作用,但我可以导入 a 然后使用 a.b

python - 如何使用 python 查找簇?

javascript - 从 Neo4j 解析复杂的 JSON 结果

javascript - 如何从 JSON 对象中读取键

encoding - 哪个编码错误将 "vóór"编码为 "v3/43/4r"?

python - 用于匹配 C 的多行预处理器语句的正则表达式

python - 如何有效地对utf-8编码的文件进行切片