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

时间:2017-05-15 10:33:02

标签: python json rest

我正在服务器上进行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"])

你能帮我解决一下这个怎么办? 谢谢。

4 个答案:

答案 0 :(得分:1)

projectNames = []

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

    data = response.json()
    for actor in data["actors"]:
        if actor["displayName"] and actor["displayName"] == "group2":
            projectNames.append(actor["id"])

答案 1 :(得分:1)

Tayyab, 你需要这样做。它会起作用。

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

答案 2 :(得分:0)

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

    group2_actors = [actor['id'] for actor in data['actors']
                     if actor['displayName'] == 'group2']
    if len(group2_actors) > 0:
        projectNames.update(group2_actors)

projectNames是一组具有displayName == group2的唯一参与者ID。

答案 3 :(得分:-1)

some_json = {}
result = [actor['id'] for actor in some_json['actors'] if actor['name']=='group2']

所以第二个json的result将为[10207]