尝试循环通过JSON时,字符串索引必须是整数

时间:2016-10-06 03:27:07

标签: python json

这就是我的JSON的样子:

{
    "Version": "2012-10-17",
    "Statement": [{
        "Sid": "xxx",
        "Effect": "Allow",
        "Principal": {
            "Service": "cloudtrail.amazonaws.com"
        },
        "Action": "s3:GetBucketAcl",
        "Resource": "arn:aws:s3:::xxx"
    },
    {
        "Sid": "yyy",
        "Effect": "Allow",
        "Principal": {
            "Service": "cloudtrail.amazonaws.com"
        },
        "Action": "s3:PutObject",
        "Resource": "arn:aws:s3:::xxx/AWSLogs/yyy/*",
        "Condition": {
            "StringEquals": {
                "s3:x-amz-acl": "bucket-owner-full-control"
            }
        }
    }]
}

我想循环通过Statement数组。这就是我的循环的样子:

for p in policy['Statement']:
   print p

这是我得到的错误:

TypeError: string indices must be integers

print(type(policy))收益<type 'unicode'>

这就是政策的样子:

{
    u'Policy': u'{
        "Version": "2012-10-17",
        "Statement": [{
            "Sid": "xxx",
            "Effect": "Allow",
            "Principal": {
                "Service": "cloudtrail.amazonaws.com"
            },
            "Action": "s3:GetBucketAcl",
            "Resource": "arn:aws:s3:::xxx"
        },
        {
            "Sid": "yyy",
            "Effect": "Allow",
            "Principal": {
                "Service": "cloudtrail.amazonaws.com"
            },
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::yyy/AWSLogs/yyy/*",
            "Condition": {
                "StringEquals": {
                    "s3:x-amz-acl": "bucket-owner-full-control"
                }
            }
        }]
    }'
}

0 个答案:

没有答案