gpt4 book ai didi

python - 使用 JSON 合并两个 API 调用

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:12 25 4
gpt4 key购买 nike

我正在通过 API 从 GET 请求中提取 JSON 数据,但无法将响应连接在一起。我是字典和索引的新手(对于这种方式也是 python)。

API数据是一样的,只是页码不同而已。能够通过 eclipse 调试器进行验证。我能够确认每个 API 调用也能够提取它需要的数据。

#pulls from the API
response = requests.get(url, headers=headers)

obj_json = response.content
objLoader = json.loads(obj_json)
obj1 = objLoader

#url changes to page 2
response = requests.get(url, headers=headers)

obj_json = response.content
obj1 = json.loads(obj_json)

newObj = dict(obj1.items() + obj2.items())
#I have also tried the following:
#dict(obj1.items() | obj2.items())

最后,我希望将 obj2 附加到 obj1。

#Let's say obj1 has:
indexes[0][1]
#and obj2 has
indexes [0][1]
#I'd like obj1 to have
indexes[0][1][2][3]

obj1 和 2 项目数据的结构如下:

obj1/2: dict u'count': 25370, u'items': [{..........}]

__len__: int: 508
count: int 25370
items: <type 'list'>: [{...}]
page: int
pages: int

谢谢,如果您需要更多信息,请告诉我!

最佳答案

让我们假设您的 JSON 响应如下所示:

{"ID":1,"name":"Donald","first-name":"Trump","age":25,"hobbies":["tweet","hunting",{"sports":["volley-ball","golf"]}],"address":{}}

对于 Obj2:

{"ID":2,"name":"Barack","first-name":"Obama","age":25,"hobbies":["reading","cinema",{"sports":["volley-ball","badminton"]}],"address":{}}

您需要将 JSON 响应转换为字典,然后将此字典附加到对象数组:

 [{"ID":1,"name":"Donald","first-name":"Trump","age":25,"hobbies":["tweet","hunting",{"sports":["volley-ball","golf"]}],"address":{}},{"ID":2,"name":"Barack","first-name":"Obama","age":25,"hobbies":["reading","cinema",{"sports":["volley-ball","badminton"]}],"address":{}}]

关于python - 使用 JSON 合并两个 API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36205458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com