解决TypeError: Object of type xxx is not JSON serializable

json.dumps()可处理的键值类型有:
str
int,
float,
(list,tuple)
dict

若键值为其他类型数据时,例如tensor、ndarray、bytes时则会报错。

解决方案:

方案1、首先将键值转换成上述合法的格式,然后再调用json.dumps()函数。

方案2、重写json.JSONEncoder中的default函数,在default函数中写入处理tensor、ndarray、bytes类型数据的代码。

你可能感兴趣的:(json,python,开发语言)