TypeError: Object of type 'bytes' is not JSON serializable
2018-02-28 10:26
633 查看
笔者将mat文件用python读取数据之后,得到一个字典数组,笔者想将这个字典数组储存到json文件中,因此应该先编码json数据,因此用到了json.dumps函数进行编码,但是我使用json.dumps函数时发现有会出现问题:TypeError: Object of type 'bytes' is not JSON serializable 后来查阅相关资料才发现,默认的编码函数很多数据类型都不能编码,因此可以自己写一个encoder去继承jsonencoder ,这样就能够进行编码了。
比如说上面的这个问题,是因为json.dumps函数发现字典里面有bytes类型的数据,因此无法编码,只要在编码函数之前写一个编码类就行了,只要检查到了是bytes类型的数据就把它转化成str类型。class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, bytes):
return str(obj, encoding='utf-8');
return json.JSONEncoder.default(self, obj) 这样就解决了这个问题。
后面在编码的时候发现出现类似问题:TypeError: Object of type 'ndarray' is not JSON serializable 这样也是一样的处理方式,当检查到了ndarray数据,把它转化成list数据就行:class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, bytes):
return str(obj, encoding='utf-8');
return json.JSONEncoder.default(self, obj) 这样就编码好了数据了。
最后再放一波参考连接,我看这个链接才明白问题所在的:点击打开链接
比如说上面的这个问题,是因为json.dumps函数发现字典里面有bytes类型的数据,因此无法编码,只要在编码函数之前写一个编码类就行了,只要检查到了是bytes类型的数据就把它转化成str类型。class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, bytes):
return str(obj, encoding='utf-8');
return json.JSONEncoder.default(self, obj) 这样就解决了这个问题。
后面在编码的时候发现出现类似问题:TypeError: Object of type 'ndarray' is not JSON serializable 这样也是一样的处理方式,当检查到了ndarray数据,把它转化成list数据就行:class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
elif isinstance(obj, bytes):
return str(obj, encoding='utf-8');
return json.JSONEncoder.default(self, obj) 这样就编码好了数据了。
最后再放一波参考连接,我看这个链接才明白问题所在的:点击打开链接
相关文章推荐
- python2.7web代码用python3.6运行时,浏览器发送请求报错TypeError: Object of type 'bytes' is not JSON serializable的解决办法
- 在使用json.dumps时遇到报错TypeError: Object of type 'float32' is not JSON serializable
- TypeError: Object of type 'float32' is not JSON serializable
- 【已解决】Object of type 'QuerySet' is not JSON serializable
- 【已解决】Object of type 'range' is not JSON serializable
- Python 3.x Error:TypeError: a bytes-like object is required, not 'str'
- python write()参数错误,导致报错TypeError: a bytes-like object is required, not 'str'
- Flask 中使用 JSON 响应页面的 Ajax 请求报错 TypeError: 'dict' object is not callable
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- 关于 "Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function" 的解决方案
- python中Base64编码和解码:TypeError: a bytes-like object is required, not 'str'
- TypeError: ObjectId('') is not JSON serializable
- Python 3.x: TypeError: a bytes-like object is required, not 'str'
- datetimeTypeError: datetime.datetime(2015, 1, 29, 8, 17, 41) is not JSON serializable
- Python中3+版本运行2+版本程序出现TypeError: a bytes-like object is required, not 'str'之读取PDF中的图片
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- Python 3.5 Socket TypeError: a bytes-like object is required, not 'str' 错误提示
- TypeError: a bytes-like object is required, not 'str'
- TypeError: a bytes-like object is required, not 'str'
- [scrapy] exceptions.TypeError:XXX is not json serializable