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


81

我想将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)

首先是第一件事。字节到字符串,然后字符串到JSON
OneCricketeer 16-10-15

我已经通过使用将字节转换为字符串,.decode("utf-8")但是当我尝试将字符串转换为JOSN时,出现此错误json.decoder.JSONDecodeError:预期值:第1行第174列(字符173)
Merouane Benthameur

您可以使用相关代码更新问题并打印出解码后的字符串吗?
OneCricketeer

1
而且,您从哪里获得此json?
OneCricketeer

6
@MerouaneBenthameur失败的原因是因为您拥有的字符串不是JSON。最明显的是JSON使用",而不是'
怪胎

Answers:


122

您的bytes对象几乎是JSON,但它使用的是单引号而不是双引号,并且它必须是字符串。因此,解决该问题的一种方法是解码bytestostr并替换引号。另一种选择是使用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)

通常,出现此问题是因为有人通过打印Pythonrepr而不是使用json模块创建正确的JSON数据来保存数据。如果可能,最好解决该问题,以便首先创建正确的JSON数据。


6
我不相信这是JSON字符串,而是Python repr,所以请改用literal_eval
Antti Haapala

顺便说一句,如果你要分析或穿越复杂的JSON结构,请参阅stackoverflow.com/a/52414034/4014959stackoverflow.com/a/41778581/4014959
PM 2Ring


0

要将字节数组直接转换为json,您可以先使用解码()将字节数组转换为字符串,utf-8是标准格式。更改引号标记。最后一步是从转储的字符串中删除“,将json对象从字符串更改为列表。

dumps(s.decode()).replace("'", '"')[1:-1]

0

Python 3.5 +使用io模块

import json
import io

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\'}]'

fix_bytes_value = my_bytes_value.replace(b"'", b'"')

my_json = json.load(io.BytesIO(fix_bytes_value))  
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.