如何在 python3 中 json.dumps 字节对象

How to json.dumps byte object in python3

在python2

import json
a = {"text": u"你好".encode("gbk")}
json.dumps(a, ensure_ascii=False)

>>> Out: '{"text": "\xc4\xe3\xba\xc3"}'

我想在 python3 中获得相同的 'Out':

import codecs
byte_obj = "你好".encode("gbk")
x = byte_obj.decode("utf8", "backslashreplace") # ops, it become '\xc4\xe3\xba\xc3'
x = codecs.escape_encode(byte_obj)[0] # ops, it become b'\xc4\xe3\xba\xc3'

# fail, I have to concatenate them

b'{"text": "' + u"你好".encode("gbk") + b'"}'

>>> Out: b'{"text": "\xc4\xe3\xba\xc3"}'

在Python3,如果有办法转换

{"text": "你好"}  # first, encoding with gbk, then json.dumps 

b'{"text": "\xc4\xe3\xba\xc3"}'  # json serialized result

如果你真的想要GBK编码在Python 3:

import json
a = {"text": u"你好"}
print(json.dumps(a, ensure_ascii=False).encode('gbk'))

b'{"text": "\xc4\xe3\xba\xc3"}'