"TypeError :(整数)不可JSON序列化"在Python中序列化JSON时? [英] "TypeError: (Integer) is not JSON serializable" when serializing JSON in Python?

查看:132
本文介绍了"TypeError :(整数)不可JSON序列化"在Python中序列化JSON时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从python发送一个简单的字典到json文件,但是我不断收到"TypeError:1425不能序列化JSON"消息.

I am trying to send a simple dictionary to a json file from python, but I keep getting the "TypeError: 1425 is not JSON serializable" message.

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文件,这是不希望的.

If I add the default argument, then it writes, but the integer values are written to the json file as strings, which is undesirable.

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

推荐答案

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

I found my problem. The issue was that my integers were actually type numpy.int64.

这篇关于"TypeError :(整数)不可JSON序列化"在Python中序列化JSON时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆