将字节对象列表转换为字典

时间:2018-07-15 11:47:25

标签: python json http python-requests

我正在使用一个API,该API返回JSON中的对象列表。但是,当我通过Requests lib获得其内容时,该内容是一个像这样的对象的字节数组:

b'[{"id":44,"id_string":"a2BPQDsGLfLiwo4r5U4JCY","title":"ED_1803_ITAIPAVA_RJ","description":"ED_1803_ITAIPAVA_RJ","url":"https://kobocat.docker.kobo.techo.org/api/v1/data/44"},{"id":57,"id_string":"a3pb3ALiGuQAHD6XzdHAip","title":"ED_v2018_1801_Taba\xc3\xa7u-SP","description":"ED_v2018_1801_Taba\xc3\xa7u-SP","url":"https://kobocat.docker.kobo.techo.org/api/v1/data/57"},{"id":68,"id_string":"a4Gz2dSwRuyQCsjBwNhf3D","title":"ECS_1804_SONHO REAL-BA","description":"ECS_1804_SONHO REAL-BA","url":"https://kobocat.docker.kobo.techo.org/api/v1/data/68"},{"id":2,"id_string":"a4KjYoy8ieCRNykiYb7nGP","title":"ECS_1708_Vila Esperan\xc3\xa7a-SP","description":"ECS_1708_Vila Esperan\xc3\xa7a-SP","url":"https://kobocat.docker.kobo.techo.org/api/v1/data/2"},{"id":38,"id_string":"a7GQQ7xEu4K6HXWYu9SaSC","title":"ECo_1711_Terra Nossa-UF","description":"ECo_1711_Terra Nossa-UF","url":"https://kobocat.docker.kobo.techo.org/api/v1/data/38"},{"id":78,"id_string":"a7NnnbdhBUSsGoxVWBiGFb","title":"ECoSP_1805_Vila Nova Esperan\xc3\xa7a-SP","description":"ECoSP_1805_Vila Nova Esperan\xc3\xa7a-SP","url":"https://kobocat.docker.kobo.techo.org/api/v1/data/78"}]

如何使它成为标准的字典字典?我尝试使用in in range()遍历字节数组,但是我只能返回数字。

3 个答案:

答案 0 :(得分:1)

您可以这样做:

import json

dic = json.loads(Your_input)

答案 1 :(得分:0)

我无法对答案发表评论,所以我写在这里。

import json  

dic = json.loads(Your_input)

此代码完美运行,

但正如评论部分中提到的@jonrsharpe,你不需要导入任何东西。

您可以使用:

page = reg.get("your_url")
page.json()

答案 2 :(得分:-1)

首先,您必须使用以下命令导入json库:

import json

然后将您的数据输出数据加载到字典中:

dic = json.loads(your_output_data)

示例:

import json
dic = json.loads(your_output_data)
print(dic)