gpt4 book ai didi

python - 在python中从JSON中检索数据,如果对象名称匹配,则存储该对象的 key

转载 作者:太空宇宙 更新时间:2023-11-03 15:14:30 25 4
gpt4 key购买 nike

我正在服务器上进行 REST 调用。第一个 REST 调用获取所有项目,并将项目的 ID 存储在数组中。下面是 JSON。例如它会返回类似这样的内容:

[
{
"expand": "description,lead,url,projectKeys",
"self": "http://localhost:8080/rest/api/2/project/10101",
"id": "10101",
"key": "GR1",
"name": "Group1Project",
"avatarUrls": {
"48x48": "http://localhost:8080/secure/projectavatar?avatarId=10324",
"24x24": "http://localhost:8080/secure/projectavatar?size=small&avatarId=10324",
"16x16": "http://localhost:8080/secure/projectavatar?size=xsmall&avatarId=10324",
"32x32": "http://localhost:8080/secure/projectavatar?size=medium&avatarId=10324"
},
"projectTypeKey": "software"
}
]

然后我循环遍历该数组并对每个项目 id (10101) 进行另一个 REST 调用。这给了我反对该项目的组/用户。例如:

{
"self": "http://localhost:8080/rest/api/2/project/10000/role/10100",
"name": "Developers",
"id": 10100,
"actors": [
{
"id": 10207,
"displayName": "group2",
"type": "atlassian-group-role-actor",
"name": "group2",
"avatarUrl": "http://localhost:8080/secure/useravatar?size=xsmall&avatarId=10123"
}
]
}

我想获取 name == group2 的所有项目 ID。

以下是我的 Python 代码,但它不起作用。

import requests

ids = []

response = requests.get('http://localhost:8080/rest/api/2/project',
auth=('*', '*'))
data = response.json()

for line in data:
ids.append(line["id"])

print(ids)


# Check if group exists in Project roles.
# If it does, then save the project name in the list of arrays.

projectNames = []

for id in ids:
url = 'http://localhost:8080/rest/api/2/project/'+id+'/role/10100'
response = requests.get(url,
auth = ('*', '*'))

data = response.json()

if data.displayName == 'group2':
projectNames.append(["id"])

您能帮我解决一下这个问题吗?谢谢。

最佳答案

塔亚布,你需要这样做。它会起作用的。

for actor in data['actors']:
if actor['displayName']=='group2':
projectNames.append(id)

关于python - 在python中从JSON中检索数据,如果对象名称匹配,则存储该对象的 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43977236/

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