在python中通过json递归

在python中通过json递归,python,json,python-2.7,python-3.x,Python,Json,Python 2.7,Python 3.x,我有一个json示例: 我想使用python的json模块并递归到pevcwebasg中的MaxSize。具有以下代码: 导入json param_file_handle = json.load(open("sample.json")) print param_file_handle['Resources']['pevcwebasg']['Type'] resources = param_file_handle['Resources'] for asg in resources: pr

我有一个json示例:

我想使用python的json模块并递归到pevcwebasg中的MaxSize。具有以下代码:

导入json

param_file_handle = json.load(open("sample.json"))
print param_file_handle['Resources']['pevcwebasg']['Type']
resources = param_file_handle['Resources']
for asg in resources:
     print asg["Type"]
其输出为:

> AWS::AutoScaling::AutoScalingGroup Traceback (most recent call last): 
> File "test.py", line 8, in <module>
>     print asg['Type'] TypeError: string indices must be integers
错误:

Traceback (most recent call last):
  File "test.py", line 9, in <module>
    print values["MaxSize"]
TypeError: string indices must be integers
这将遍历资源的键,而不是值。尝试:

for asg in resources.values():
这将遍历资源的键,而不是值。尝试:

for asg in resources.values():

你破坏了雇佣关系,错过了pevcwebasg

resources = param_file_handle['Resources']['pevcwebasg']
for asg in resources:
     print asg["Type"]

你破坏了雇佣关系,错过了pevcwebasg

resources = param_file_handle['Resources']['pevcwebasg']
for asg in resources:
     print asg["Type"]
试试这个


试试这个。

下次,请粘贴文本而不是发布图片。这是一个巨大的json,不想占用整个空间,所以只使用图片。我知道我本来可以修剪的。下一次,请粘贴文本而不是发布图片。这是一个巨大的json,不想占用整个空间,所以只使用图片。我知道我本来可以修剪的。可以。我会做键而不是值,这样你就可以选择复制符合你标准的键的内容。@rajesh.kanakabandi和帖子的所有者-看看编辑你不需要内部for循环。当您想要访问MaxSize时。我会使用键而不是值,这样您就可以选择复制符合您标准的键的内容。@rajesh.kanakabandi和帖子的所有者-查看编辑器您不需要内部for循环。当您想要访问MaxSize时。
param_file_handle = json.load(open("sample.json"))
resources = param_file_handle['Resources']
for asg in resources.values():
     if asg["Type"] == "AWS::AutoScaling::AutoScalingGroup":
          print asg["Properties"]["MaxSize"]