遍历json数组并组合值

时间:2020-01-15 18:24:56

标签: python json python-3.x

这是我的示例JSON数组:

{
  "repository": "sample-repo-url",
  "stringsFound": {
    "0": ["test-stringsFound0"],
    "1": ["test-stringsFound1"],
    "2": ["test-stringsFound2"]
  },
  "reason": {
    "0": ["test-reason0"],
    "1": ["test-reason1"],
    "2": ["test-reason2"]
  },
  "path": {
    "0": ["test-path0"],
    "1": ["test-path1"],
    "2": ["test-path2"]
  }
}

我需要能够将reasonpathstringsFound的值组合起来并打印出来。

例如:

test-stringsFound0 -- test-reason0 -- test-path0
test-stringsFound0 -- test-reason1 -- test-path1

以此类推。

我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

这应该对您有用:

for each in zip(d['stringsFound'].values(), d['reason'].values(), d['path'].values()):
    print(f'{each[0][0]} -- {each[1][0]} -- {each[2][0]}')