我有一个嵌套的字典,我尝试使用json.dumps(unserialized_data), indent=2)
将其转换为JSON。
{
"status": "SUCCESS",
"data": {
"cal": [
{
"year": 2022,
"month": 8,
"a": [
{
"a_id": 1,
"b": [
{
"abc_id": 1,
"val": 2342
}
]
}
]
},
{
"year": 2022,
"month": 9,
"a": [
{
"a_id": 2,
"b": [
{
"abc_id": 3,
"val": 2342
}
]
}
]
}
]
}
}
如何将int64
类型的所有整数转换为int
,同时不影响dict的结构和任何其他数据类型的值?
2条答案
按热度按时间b4qexyjb1#
如果blhsing的条件不适用,可以递归地深入字典,并将任何
np.int64
转换为int
:from_types
和to_types
是容器,其中相应的元素给予了要转换的源类型和要转换的目标类型。通过此函数运行字典,然后将其转储到json:将字典打印为JSON:
在线试用
nbysray52#
如果您的dict中唯一不能进行JSON序列化的对象都是
int64
类型,那么您可以通过将int
设置为默认函数来转换JSON不能序列化的对象,从而轻松地将它们序列化: