Python JSON从响应中获取数据

时间:2017-12-18 11:27:09

标签: python json

经过大量搜索后,我认为我的答案只有我答案的一半 当我得到一个json响应时就像这样

{
    "queryResponse": {
        "@last": 3,
        "@first": 0,
        "@count": 4,
        "@type": "ClientDetails",
        "@requestUrl": "https://1.1.1.1/webacs/api/v2/data/ClientDetails?userName=contains("usertest")",
        "@responseType": "listEntityIds",
        "@rootUrl": "https://1.1.1.1/webacs/api/v2/data",
        "entityId": [
            {
                "@type": "ClientDetails",
                "@url": "https://1.1.1.1/webacs/api/v2/data/ClientDetails/236551459",
                "$": "236551459"
            },
            {
                "@type": "ClientDetails",
                "@url": "https://1.1.1.1/webacs/api/v2/data/ClientDetails/267361256",
                "$": "267361256"
            },
            {
                "@type": "ClientDetails",
                "@url": "https://10.141.1.29/webacs/api/v2/data/ClientDetails/370079361",
                "$": "370079361"
            },
            {
                "@type": "ClientDetails",
                "@url": "https://1.1.1.1/webacs/api/v2/data/ClientDetails/501402176",
                "$": "501402176"
            }
        ]
    }
}

因为我想获得@url的列表,但我刚刚能够获得entityId

json_obj = response.json()
object = json_obj['queryResponse']
entityId = object['entityId']
print(entityId)

我无法从entityId打印,但我无法在每个@url字段中获得该字段。任何帮助都将非常感激。

2 个答案:

答案 0 :(得分:2)

由于返回键值对,您应该按顺序解析每个键值对。

key: The index of the selected menu item

希望这会有所帮助。 :)

答案 1 :(得分:1)

for entity in entityId: 
    print(entity["@url"])