2012-11-03 39 views
0

我有這個json對象,我很好奇如何遍歷servicecatalog:名稱和警報的任何名稱不等於「service-foo」或「服務欄」。如何通過JSON對象搜索特定列表o預期的鍵值?

這裏是我的JSON對象:

{ 
    "access": { 
     "serviceCatalog": [ 
      { 
       "endpoints": [ 
        { 
         "internalURL": "https://snet-storage101.example.com//v1.0", 
         "publicURL": "https://storage101.example.com//v1.0", 
         "region": "LON", 
         "tenantId": "1 
        }, 
        { 
         "internalURL": "https://snet-storage101.example.com//v1.0", 
         "publicURL": "https://storage101.example.com//v1.0", 
         "region": "USA", 
         "tenantId": "1 
        } 
       ], 
       "name": "service-foo", 
       "type": "object-store" 
      }, 
      { 
       "endpoints": [ 
        { 
         "publicURL": "https://x.example.com:9384/v1.0/x", 
         "tenantId": "6y5t4re32" 
        } 
       ], 
       "name": "service-bar", 
       "type": "rax:test" 
      }, 
      { 
       "endpoints": [ 
        { 
         "publicURL": "https://y.example.com:9384/v1.0/x", 
         "tenantId": "765432" 
        } 
       ], 
       "name": "service-thesystem", 
       "type": "rax:test" 
      } 
     ] 
} 
+1

您在JSON中出現錯誤「tenantId」:「1'末尾缺少'」',末尾缺少'}'。 – Bula

回答

0

如果x是上面提到的字典。你可以做

for item in x["access"]["serviceCatalog"]: 
    if item["name"] not in ["service-foo", "service-bar"]: 
     print(item["name"]) 

PS:你可以使用json.loads()如果你問該解碼JSON數據。而且你的JSON也有錯誤。

+0

現在修復:)。謝謝。 – tarashish

相關問題