如何使用python根据条件从json响应中提取特定值?

0yycz8jy  于 2023-04-22  发布在  Python
关注(0)|答案(1)|浏览(136)

在执行GET调用后,我得到了一个json响应,我需要从该json响应中获取一个特定的值。

{
    "identifier": "id",
    "items": [
        {
            "channelGroupId": 26,
            "controlMethod": "MANUAL",
            "portChannelId": 0,
            "ethernetFecMode": "NONE",
            "ethernetLoopback": "NONE",
            "isSwitchablePort": false,
            "negotiationMode": "UNKNOWN",
            "configuredDuplexMode": "AUTO_NEGOTIATE",
            "isConnectorPresent": "FALSE",
            "mtu": 1514,
            "delay": 0,
            "isPromiscuous": false,
            "isSpanMonitored": false,
            "ifSpeed": {
                "longAmount": 0
            },
            "isFloatingPep": false,
            "isInMaintenance": false,
            "adminStatus": "UP",
            "operStatus": "DOWN",
            "type": "PROPVIRTUAL",
            "name": "Bundle-Ether26",
            "id": 27423442,          #I want to get this value if the channelGroupId = 26
            "uuid": "5cc095bb-e67d-4827-8a46-c3d2bf9d450f",
            "displayName": "f03d09bc[4427434_10.104.120.141,Bundle-Ether26]"
        },
        {
            "channelGroupId": 36,
            "controlMethod": "MANUAL",
            "portChannelId": 0,
            "ethernetFecMode": "NONE",
            "ethernetLoopback": "NONE",
            "isSwitchablePort": false,
            "negotiationMode": "UNKNOWN",
            "configuredDuplexMode": "AUTO_NEGOTIATE",
            "isConnectorPresent": "FALSE",
            "mtu": 1514,
            "delay": 0,
            "isPromiscuous": false,
            "isSpanMonitored": false,
            "ifSpeed": {
                "longAmount": 0
            },
            "isFloatingPep": false,
            "isInMaintenance": false,
            "adminStatus": "UP",
            "operStatus": "DOWN",
            "type": "PROPVIRTUAL",
            "name": "Bundle-Ether36",
            "id": 27423438,
            "uuid": "fff16259-4079-4d67-bdf9-c1a4720f01c3",
            "displayName": "f03d09bc[4427434_10.104.120.141,Bundle-Ether36]"
        },
        {
            "channelGroupId": 21,
            "controlMethod": "MANUAL",
            "portChannelId": 0,
            "ethernetFecMode": "NONE",
            "ethernetLoopback": "NONE",
            "isSwitchablePort": false,
            "negotiationMode": "UNKNOWN",
            "configuredDuplexMode": "AUTO_NEGOTIATE",
            "isConnectorPresent": "FALSE",
            "mtu": 1514,
            "delay": 0,
            "isPromiscuous": false,
            "isSpanMonitored": false,
            "ifSpeed": {
                "longAmount": 0
            },
            "isFloatingPep": false,
            "isInMaintenance": false,
            "adminStatus": "UP",
            "operStatus": "DOWN",
            "type": "PROPVIRTUAL",
            "name": "Bundle-Ether21",
            "id": 27423440,
            "uuid": "40c11ffd-34cd-4e80-be1d-58899dba4894",
            "displayName": "f03d09bc[4427434_10.104.120.141,Bundle-Ether21]"
        }
    ],
    "status": {
        "statusCode": 0,
        "statusMessage": "Get operation is successful",
        "hideDialog": true
    }
}

我想提取id的值(即27423440),在同一个字典中,channelGroupId的值为26
我尝试了以下代码,但无法访问字典

if response.status_code == 200:
                if "success" in response.text:
                    #print(response.text)
                    full_response = response.json()
                    list_of_pagp = full_response['items']
                    print(list_of_pagp)
                    for i in list_of_pagp:
                        if(list_of_pagp['channelGroupId'] == 26 ):
                            instanceId = list_of_pagp[id]
                            break
                    print(instanceId)

不知道我错过了什么。

tcomlyy6

tcomlyy61#

if response.status_code == 200:
            if "success" in response.text:
                #print(response.text)
                full_response = response.json()
                list_of_pagp = full_response['items']
                print(list_of_pagp)
                for i in list_of_pagp:
                    if i['channelGroupId'] == 26:  #here is use i not list_of_pagp
                        instanceId = list_of_pagp['id']
                        break
                print(instanceId)

似乎只是个低级的错误,
从以下位置修复:

if(list_of_pagp['channelGroupId'] == 26 ):

收件人:

if(i['channelGroupId'] == 26 ):

相关问题