2017-04-17 93 views
-2

這是我的JSON數據:如何在Python中迭代JSON列表?

{ 
 
    "AccountRegion": "testaccount", 
 
    "AssumeRole": "arn", 
 
    "Policies": [ 
 
    "hello", 
 
    "world" 
 
    ], 
 
    "Region": "usa" 
 
}, 
 
{ 
 
    "AccountRegion": "testaccount-2", 
 
    "AssumeRole": "arn", 
 
    "Policies": "anotherpolicy", 
 
    "Region": "usa" 
 
}

我無法弄清楚如何遍歷JSON。我想遍歷數據並獲取數據集中每個成員的策略。我怎樣才能做到這一點?

+0

你嘗試過這麼遠嗎? –

+0

看起來副本的http://stackoverflow.com/questions/20199126/reading-json-from-a-file –

+0

我已經把json數據放在一個變量中,我已經通過這樣的json循環,沒有成功 'for數據['Items']: region = data ['Region'] assumerole = data ['AssumeRole'] policies = data ['Policies'] for table_scan ['Items'] ['policies' ]: print policy' –

回答

0

試試這個。

arr = [{ 
     "AccountRegion": "testaccount", 
     "AssumeRole": "arn", 
     "Policies": [ 
     "hello", 
     "world" 
     ], 
     "Region": "usa" 
    }, 
    { 
     "AccountRegion": "testaccount-2", 
     "AssumeRole": "arn", 
     "Policies": "anotherpolicy", 
     "Region": "usa" 
    }] 

for x in range(0,len(arr)): 
    print arr[x]['AccountRegion'] 
0

你可以試試這個

import json 
parsedJson = json.loads(data) 
for i in parsedJson: 
    print parsedJson["Policies"]