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

时间:2016-10-15 13:34:16

标签: python json

我想将字节数组转换为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:期望值:第1行第174列(字符173)

4 个答案:

答案 0 :(得分:51)

您的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)

<强>输出

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

答案 1 :(得分:1)

您可以简单地使用

import json

json.loads(my_bytes_value)

答案 2 :(得分:0)

要将此bytesarray直接转换为json,您可以先将bytesarray转换为带有decode()的字符串,utf-8是标准的。更改引号标记。最后一步是从转储的字符串中删除“将json对象从字符串更改为列表。

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

答案 3 :(得分:0)

Python 3.5 + 使用io module

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