简体   繁体   English

Python JSON从响应中获取数据

[英]Python JSON get data from response

after searching a lot I think I have just half of the answer to my problem When I got a json response it is like this 经过大量的搜索,我认为我只有一半的问题得到答案当我得到一个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"
            }
        ]
    }
}

Since I want to get a list of @url but I've just been able to get to entityId 因为我想获得@url的列表,但我刚刚能够访问entityId

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

And I cant print from entityId but I am not able to get just the field every @url field. 我无法从entityId打印,但我无法在每个@url字段中获得该字段。 Any help would be very appreciated. 任何帮助将非常感激。

Since here key-value pairs are returned you should parse each one sequentially. 由于返回键值对,您应该按顺序解析每个键值对。

import json
from pprint import pprint

json_obj = response.json()

entityId = json_obj['queryResponse']['entityId']

for _ in entityId:
    pprint( _['@url'])

Hope this helps. 希望这可以帮助。 :) :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM