Python-将字节数组转换为JSON格式

我想将bytes数组转换为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\'}]'

这是我想要的期望结果:

[{

"Date": "2016-05-21T21:35:40Z",

"CreationDate": "2012-05-05",

"LogoType": "png",

"Ref": 164611595,

"Classes": [

"Email addresses",

"Passwords"

],

"Link": "http://some_link.com"}]


首先,我将字节转换为字符串:

my_new_string_value = my_bytes_value.decode("utf-8")

但是当我尝试加载到JSON时:

my_json = json.loads(my_new_string_value)

我收到此错误:

json.decoder.JSONDecodeError: Expecting value: line 1 column 174 (char 173)

回答:

您的bytes对象 几乎 是JSON,但是它使用单引号而不是双引号,并且它必须是字符串。因此,解决该问题的一种方法是解码bytesto

str并替换引号。另一种选择是使用ast.literal_eval;

有关详情,请参见下文。如果要打印结果或将其作为有效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)

[{"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

}

]


正如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)


通常,出现此问题是因为有人通过打印Python

repr而不是使用json模块创建正确的JSON数据来保存数据。如果可能的话,最好解决该问题,以便首先创建正确的JSON数据。

以上是 Python-将字节数组转换为JSON格式 的全部内容, 来源链接: utcz.com/qa/432237.html

回到顶部