Python - 将字节数组转换为 JSON 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40059654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python - Convert a bytes array into JSON format
提问by Merouane Benthameur
I want to convert a bytes array to JSON format, this the source I have:
我想将字节数组转换为 JSON 格式,这是我拥有的源:
my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\', \'Passwords\'],\'Link\':\'http://some_link.com\'}]'
and this is the desired outcome i want to have :
这是我想要的结果:
[{
"Date": "2016-05-21T21:35:40Z",
"CreationDate": "2012-05-05",
"LogoType": "png",
"Ref": 164611595,
"Classes": [
"Email addresses",
"Passwords"
],
"Link": "http://some_link.com"}]
thanks for your help
感谢您的帮助
EDIT:
编辑:
First, I converted the bytes to string :
首先,我将字节转换为字符串:
my_new_string_value = my_bytes_value.decode("utf-8")
but when I try to loads to JSON : my_json = json.loads(my_new_string_value)
I get this error :
json.decoder.JSONDecodeError: Expecting value: line 1 column 174 (char 173)
但是当我尝试加载到 JSON 时:my_json = json.loads(my_new_string_value)
我收到此错误:json.decoder.JSONDecodeError: Expecting value: line 1 column 174 (char 173)
回答by PM 2Ring
Your bytes
object is almostJSON, but it's using single quotes instead of double quotes, and it needs to be a string. So you just need to decode it and replace the quotes. If you want to print it or save it to a file as valid JSON you can load the JSON to a Python list and then dump it out. Eg,
您的bytes
对象几乎是JSON,但它使用单引号而不是双引号,并且它需要是一个字符串。所以你只需要解码它并替换引号。如果您想打印它或将其作为有效的 JSON 保存到文件中,您可以将 JSON 加载到 Python 列表中,然后将其转储出来。例如,
import json
my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\', \'Passwords\'],\'Link\':\'http://some_link.com\'}]'
# Decode UTF-8 bytes to Unicode, and convert single quotes
# to double quotes to make it valid JSON
my_json = my_bytes_value.decode('utf8').replace("'", '"')
print(my_json)
print('- ' * 20)
# Load the JSON to a Python list & dump it back out as formatted JSON
data = json.loads(my_json)
s = json.dumps(data, indent=4, sort_keys=True)
print(s)
output
输出
[{"Date": "2016-05-21T21:35:40Z", "CreationDate": "2012-05-05", "LogoType": "png", "Ref": 164611595, "Classe": ["Email addresses", "Passwords"],"Link":"http://some_link.com"}]
- - - - - - - - - - - - - - - - - - - -
[
{
"Classe": [
"Email addresses",
"Passwords"
],
"CreationDate": "2012-05-05",
"Date": "2016-05-21T21:35:40Z",
"Link": "http://some_link.com",
"LogoType": "png",
"Ref": 164611595
}
]
As Antti Haapala mentions in the comments, we can use ast.literal_eval
to convert my_bytes_value
to a Python list, once we've decoded it to a string.
正如 Antti Haapala 在评论中提到的,一旦我们将其解码为字符串,我们就可以使用ast.literal_eval
将其转换my_bytes_value
为 Python 列表。
from ast import literal_eval
import json
my_bytes_value = b'[{\'Date\': \'2016-05-21T21:35:40Z\', \'CreationDate\': \'2012-05-05\', \'LogoType\': \'png\', \'Ref\': 164611595, \'Classe\': [\'Email addresses\', \'Passwords\'],\'Link\':\'http://some_link.com\'}]'
data = literal_eval(my_bytes_value.decode('utf8'))
print(data)
print('- ' * 20)
s = json.dumps(data, indent=4, sort_keys=True)
print(s)
回答by Simon
To convert this bytesarray directly to json, you could first convert the bytesarray to a string with decode(), utf-8 is standard. Change the quotation markers.. The last step is to remove the " from the dumped string, to change the json object from string to list.
要将这个 bytesarray 直接转换为 json,您可以先使用 decode() 将 bytesarray 转换为字符串,utf-8 是标准的。更改引号.. 最后一步是从转储的字符串中删除 ",将 json 对象从字符串更改为列表。
dumps(s.decode()).replace("'", '"')[1:-1]