首页 > 解决方案 > 将字节对象列表转换为 dict

问题描述

我正在使用一个 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"}]

我怎样才能使它成为一个正常的字典列表?我尝试使用 for in range() 遍历字节数组,但我只能返回数字。

标签: pythonjsonhttppython-requests

解决方案


你可以这样做:

import json

dic = json.loads(Your_input)

推荐阅读