[英]Reading in nested data from json file
I am trying to read the nested data of a json file, particularly in the example below I want to read both of the "pose" tags of -each- frame until the end.我正在尝试读取 json 文件的嵌套数据,特别是在下面的示例中,我想读取 -each-frame 的两个“pose”标签直到结束。 When I try to reach them using the script below:
当我尝试使用以下脚本联系他们时:
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'])
I get the error:我收到错误:
for data in obj['skeleton']['frame_index']:
KeyError: 'skeleton'
and when I try this:当我尝试这个时:
for data in obj['data']['frame_index']:
print(data['pose'])
It says它说
for data in obj['data']['skeleton']['frame_index']:
TypeError: list indices must be integers or slices, not str
Sample json file:示例 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
}
Given the json
you provide, this would be the path you should use to address the issue:鉴于您提供的
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'])
Remember, jsons can be treated as dictionaries that contain dictionaries and lists within them, therefore you can simply address them as such.请记住,jsons 可以被视为包含字典和列表的字典,因此您可以简单地这样处理它们。
Output:输出:
[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]
You unpack your json wrong.你解压你的json错了。 The value of
'data'
is a list. 'data'
的值是一个列表。 So you need to iterate over it.所以你需要迭代它。 Moreover, each element in it contains a list under a name
'skeleton'
:此外,其中的每个元素都包含一个名为
'skeleton'
的列表:
for data_element in obj['data']:
print(data_element['frame_index'])
for pose_element in data_element['skeleton']:
print(pose_element['pose'])
You can do this:你可以这样做:
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'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.