TypeError: Object of type 'bytes' is not JSON serializable

转载自: https://blog.csdn.net/weixin_41951954/article/details/124838931

 

代码

import json


class BytesEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8')
        return json.JSONEncoder.default(self, obj)


o = b'111'

res = json.dumps(o, ensure_ascii=False)
# res = json.dumps(o, ensure_ascii=False, cls=BytesEncoder)

print(res, type(res))

 

 

 

 

posted @ 2022-08-31 18:41  tslam  阅读(1248)  评论(0编辑  收藏  举报