2017-12-18 141 views
1

數據搜索了很多,我認爲在我剛纔的答案的一半我的問題 當我有一個JSON響應就是這個樣子Python的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。任何幫助將非常感激。

+0

基本上你需要循環到entityId並獲取特定對象來讀取和訪問它的索引。 –

回答

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

由於在這裏鍵值對被返回,所以你應該逐一解析每一個。

import json 
from pprint import pprint 

json_obj = response.json() 

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

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

希望這會有所幫助。 :)

+1

如果你真的要使用循環變量,不要使用'_'作爲循環變量。 –

+0

謝謝,COLDSPEED,我是python的新手。只是很好奇,我可以知道爲什麼不推薦使用_? –

+0

當您想要「扔掉」循環變量時(即,如果您不打算在循環體中使用它),將使用'_'。 –