本文介绍了Python-将字节数组转换为JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字节数组转换为JSON格式,这是我拥有的来源:

I want to convert a bytes array to JSON format, this the source I have:

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"}]

感谢您的帮助

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

First, I converted the bytes to string :

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)

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)

推荐答案

您的bytes对象是几乎 JSON,但是它使用单引号而不是双引号,并且它必须是字符串.因此,您只需要解码它并替换引号即可.如果要打印或将其作为有效JSON保存到文件中,可以将JSON加载到Python列表中,然后将其转储出去.例如,

Your bytes object is almost JSON, 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,

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在评论中提到的那样,一旦我们将my_bytes_value解码为字符串,就可以使用ast.literal_evalmy_bytes_value转换为Python列表.


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.

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-将字节数组转换为JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:01