这是想要将数据写到json
文件遇到的问题,Object of type 'int32/int64/ndarray' is not JSON serializable
,归根结底都是一个问题。
解决方案
由于numpy.array
的数据类型不符合json
的解码类型,使用tolist()
方法将array
转换成list
。
NOTE: 不能使用list()
方法,list()
转换的list不会改变array
的数据类型。
问题分析
先看json可以解码的数据类型, Python操作json的标准api库:
JSON | Python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
支持的数据类型基本都是常见的int
,str
类型,而numpy.array
的数据类型都是numpy
内置的类型。
import numpy as np
import json
y = []
for i in range(5):
x = np.random.randint(10, size=(5,))
y.append(x)
print(y)
d = {}
d["y"] = y
with open("res.json", "w") as f:
json.dump(d, f)
Output:
[array([4, 6, 8, 8, 1]),
array([2, 0, 9, 7, 1]),
array([8, 7, 9, 7, 4]),
array([2, 1, 2, 7, 2]),
array([6, 4, 9, 3, 3])]
TypeError: Object of type 'ndarray' is not JSON serializable
ndarray
类型不能保存,list
总能保存吧,所以先尝试list()
方法。
y = []
for i in range(5):
x = np.random.randint(10, size=(5,))
y.append(list(x)
print(y)
Output:
[[7, 9, 6, 8, 1],
[2, 8, 6, 7, 8],
[8, 4, 2, 4, 3],
[8, 0, 4, 2, 5],
[7, 1, 4, 7, 0]]
看似没有任何问题,但是每次保存json
都会遇到Object of type 'int32' is not JSON serializable
的问题,参考json可以解码的数据类型,查看x
中的数据类型。
x = np.random.randint(10, size=(5,))
x = list(x)
print(type(x[0]))
Output:
<class 'numpy.int32'>
因此判断是numpy.array
的数据类型的问题,所以尝试tolist()
方法。
x = np.random.randint(10, size=(5,))
x = x.tolist()
print(type(x[0]))
Output:
<class 'int'>
符合Json解码的标准,保存文件成功。
问题
tolist()
方法和list()
方法的区别没查到,为什么一个改变数据类型,一个未改变数据类型。