Python 循环嵌套数组

标签 python arrays for-loop iteration

我有一个如下所示的 json:

[  
   {  
      "status":"passed",
      "elements":[{"name":"foo"},{"name":"bar"}]
   },
   {  
      "status":"failed",
      "elements":[{"name":"foo1"},{"name":"bar1"}]
   }
]

我正在尝试迭代元素数组:

for a in json['elements']:
   print a['name']

我收到此错误:

TypeError: list indices must be integers, not str

我的Python实在是太糟糕了。谢谢

最佳答案

试试这个:

for a in json:
   for b in a['elements']:
       print b['name']

关于Python 循环嵌套数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41383215/

相关文章:

arrays - Segue 不传输数组

java - 没有 for 循环会在 for 循环内部运行。素数测试

c++ - 新的 C++11 range-for (foreach) 语法 : which compilers support it?

python 在 Debug模式下在 c++ 中工作,但在 exe 文件中不工作

python - 如何增加 facebook Insights api 光标的限制

python - Unicode解码错误: 'charmap' codec can't encode character X at position Y: character maps to undefined

java - 优化 Java 中的 for 循环有用吗?

python - 每次pandas数据框中出现NaN时如何重置累积和?

c# - 数组索引越界错误C#

PHP:数组可以有一个数组作为键值对中的键吗?