从json文件读取嵌套数据

时间:2019-12-26 13:51:18

标签: python json

我正在尝试读取json文件的嵌套数据,尤其是在下面的示例中,我想读取-each- frame的两个“ pose”标签,直到结束。当我尝试使用以下脚本与他们联系时:

expjsonFiles = glob.glob(expFilesPath + '*.json')

for expjsonFile in expjsonFiles[:-1]:
    jsonfpath = os.path.abspath(expjsonFile)
    jsonfname = os.path.basename(jsonfpath)

    with open(jsonfpath, 'r') as jsonf:
        json_data = jsonf.read()

    #parse json file
    obj = json.loads(json_data)
    #print(str(obj['label']))
    label = str(obj['label'])

    for data in obj['frame_index']:
        print(data['pose'])

我得到了错误:

    for data in obj['skeleton']['frame_index']:
KeyError: 'skeleton'

,当我尝试此操作时:

for data in obj['data']['frame_index']:
        print(data['pose'])

它说

for data in obj['data']['skeleton']['frame_index']:
TypeError: list indices must be integers or slices, not str

示例json文件:

{
  "data": [
    {
      "frame_index": 1,
      "skeleton": [
        {
          "pose": [
            0.2203125,
            0.2546875,
            0.271875,
            0.2421875,
            0.303125,
            0.240625,
            0.3296875,
            0.2421875,
            0.3390625,
            0.2796875
          ],
          "score": [
            1,
            1,
            1,
            1,
            1
          ]
        },
        {
          "pose": [
            0.2203125,
            0.2546875,
            0.271875,
            0.2421875,
            0.303125,
            0.240625,
            0.3296875,
            0.2421875,
            0.3390625,
            0.2796875
          ],
          "score": [
            1,
            1,
            1,
            1,
            1
          ]
        }
      ]
    },
    {
      "frame_index": 2,
      "skeleton": [
        {
          "pose": [
            0.2203125,
            0.2546875,
            0.271875,
            0.2421875,
            0.303125,
            0.240625,
            0.3296875,
            0.2421875,
            0.3390625,
            0.2796875
          ],
          "score": [
            1,
            1,
            1,
            1,
            1
          ]
        },
        {
          "pose": [
            0.2203125,
            0.2546875,
            0.271875,
            0.2421875,
            0.303125,
            0.240625,
            0.3296875,
            0.2421875,
            0.3390625,
            0.2796875
          ],
          "score": [
            1,
            1,
            1,
            1,
            1
          ]
        }
      ]
    },
    {
      "frame_index": 3,
      "skeleton": [
        {
          "pose": [
            0.2203125,
            0.2546875,
            0.271875,
            0.2421875,
            0.303125,
            0.240625,
            0.3296875,
            0.2421875,
            0.3390625,
            0.2796875
          ],
          "score": [
            1,
            1,
            1,
            1,
            1
          ]
        },
        {
          "pose": [
            0.2203125,
            0.2546875,
            0.271875,
            0.2421875,
            0.303125,
            0.240625,
            0.3296875,
            0.2421875,
            0.3390625,
            0.2796875
          ],
          "score": [
            1,
            1,
            1,
            1,
            1
          ]
        }
      ]
    }
  ],
  "label": "G1",
  "label_index": 0
}

3 个答案:

答案 0 :(得分:1)

鉴于您提供的json,这将是您应该用来解决该问题的路径:

import pandas as pd

data = {"data": [{"frame_index": 1, "skeleton": [{"pose": [0.2203125,0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]},{"pose": [0.2203125, 0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]}]},{"frame_index": 2, "skeleton": [{"pose": [0.2203125,0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]},{"pose": [0.2203125, 0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]}]},{"frame_index": 3, "skeleton": [{"pose": [0.2203125,0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]},{"pose": [0.2203125, 0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]}]}],"label": "G1", "label_index": 0}

for i in range(len(data['data'])):
    for j in range(len(data['data'][i]['skeleton'])):
        print(data['data'][i]['skeleton'][j]['pose'])

请记住,可以将jsons视为包含字典和列表的字典,因此您可以这样简单地解决它们。

输出:

[0.2203125, 0.2546875, 0.271875, 0.2421875, 0.303125, 0.240625, 0.3296875, 0.2421875, 0.3390625, 0.2796875]
[0.2203125, 0.2546875, 0.271875, 0.2421875, 0.303125, 0.240625, 0.3296875, 0.2421875, 0.3390625, 0.2796875]
[0.2203125, 0.2546875, 0.271875, 0.2421875, 0.303125, 0.240625, 0.3296875, 0.2421875, 0.3390625, 0.2796875]
[0.2203125, 0.2546875, 0.271875, 0.2421875, 0.303125, 0.240625, 0.3296875, 0.2421875, 0.3390625, 0.2796875]
[0.2203125, 0.2546875, 0.271875, 0.2421875, 0.303125, 0.240625, 0.3296875, 0.2421875, 0.3390625, 0.2796875]
[0.2203125, 0.2546875, 0.271875, 0.2421875, 0.303125, 0.240625, 0.3296875, 0.2421875, 0.3390625, 0.2796875]

答案 1 :(得分:1)

您解压缩json错了。 'data'的值是一个列表。因此,您需要对其进行迭代。此外,其中的每个元素都包含一个名称为'skeleton'的列表:

    for data_element in obj['data']:
        print(data_element['frame_index'])
        for pose_element in data_element['skeleton']:
            print(pose_element['pose'])

答案 2 :(得分:1)

您可以这样做:

data = {"data": [{"frame_index": 1, "skeleton": [{"pose": [0.2203125,0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]},{"pose": [0.2203125, 0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]}]},{"frame_index": 2, "skeleton": [{"pose": [0.2203125,0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]},{"pose": [0.2203125, 0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]}]},{"frame_index": 3, "skeleton": [{"pose": [0.2203125,0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]},{"pose": [0.2203125, 0.2546875,0.271875,0.2421875,0.303125,0.240625,0.3296875,0.2421875,0.3390625,0.2796875], "score": [1,1,1,1,1]}]}],"label": "G1", "label_index": 0}

for dict_rows in data["data"]:
    for skeleton in dict_rows['skeleton']:
        print(skeleton['pose'])