如何在Python中迭代JSON列表?

时间:2017-04-17 10:44:24

标签: json python-2.7 list

这是我的json数据:

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

我无法弄清楚如何遍历JSON。我想遍历数据并获取数据集中每个成员的策略。我怎么能做到这一点?

2 个答案:

答案 0 :(得分: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']

答案 1 :(得分:0)

你可以试试这个

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