以JSON序列化base64编码的数据
我正在编写脚本以自动生成演示数据,并且需要在JSON中序列化一些数据。该数据的一部分是图像,因此我在base64中对其进行了编码,但是当我尝试运行脚本时,我得到了:
Traceback (most recent call last): File "lazyAutomationScript.py", line 113, in <module>
json.dump(out_dict, outfile)
File "/usr/lib/python3.4/json/__init__.py", line 178, in dump
for chunk in iterable:
File "/usr/lib/python3.4/json/encoder.py", line 422, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/lib/python3.4/json/encoder.py", line 396, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.4/json/encoder.py", line 396, in _iterencode_dict
yield from chunks
File "/usr/lib/python3.4/json/encoder.py", line 429, in _iterencode
o = _default(o)
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: b'iVBORw0KGgoAAAANSUhEUgAADWcAABRACAYAAABf7ZytAAAABGdB...
...
BF2jhLaJNmRwAAAAAElFTkSuQmCC' is not JSON serializable
据我所知,无论使用base64编码的任何内容(在这种情况下,都是PNG图像)都只是一个字符串,因此它应该对序列化造成问题。我想念什么?
回答:
您必须注意数据类型。
如果读取二进制图像,则会得到字节。如果将这些字节编码为base64,则将再次获得…字节!(请参阅b64encode上的文档)
json无法处理原始字节,这就是您收到错误的原因。
我刚刚写了一些带有注释的示例,希望对您有所帮助:
from base64 import b64encodefrom json import dumps
ENCODING = 'utf-8'
IMAGE_NAME = 'spam.jpg'
JSON_NAME = 'output.json'
# first: reading the binary stuff
# note the 'rb' flag
# result: bytes
with open(IMAGE_NAME, 'rb') as open_file:
byte_content = open_file.read()
# second: base64 encode read data
# result: bytes (again)
base64_bytes = b64encode(byte_content)
# third: decode these bytes to text
# result: string (in utf-8)
base64_string = base64_bytes.decode(ENCODING)
# optional: doing stuff with the data
# result here: some dict
raw_data = {IMAGE_NAME: base64_string}
# now: encoding the data to json
# result: string
json_data = dumps(raw_data, indent=2)
# finally: writing the json string to disk
# note the 'w' flag, no 'b' needed as we deal with text here
with open(JSON_NAME, 'w') as another_open_file:
another_open_file.write(json_data)
以上是 以JSON序列化base64编码的数据 的全部内容, 来源链接: utcz.com/qa/429458.html