繁体   English   中英

访问python JSON中的嵌套项

[英]Access nested items in python JSON

我正在加载具有以下结构的 JSON 文件:

{"questions": 
     [{"question_id": 0, "image_filename": "CLEVR_val_009641.png", "question": "Are there any tiny green things made of the same material as the small brown sphere?"}, 
     {"question_id": 1, "image_filename": "CLEVR_val_010849.png", "question": "Are there any purple metal things of the same size as the green shiny object?"}, 
     {"question_id": 2, "image_filename": "CLEVR_val_004213.png", "question": "Is there a big thing of the same color as the big metal ball?"}, 
     {"question_id": 3, "image_filename": "CLEVR_val_012597.png", "question": "There is a big metallic thing to the right of the blue rubber object; are there any small cyan rubber blocks that are to the right of it?"}, 
     {"question_id": 4, "image_filename": "CLEVR_val_006304.png", "question": "Is there any other thing that has the same material as the small brown object?"}
     ]
}

我正在使用json.loads()加载文件。

到目前为止,使用:

train_data_jsonload = json.load(f)
train_data = train_data_jsonload.get("questions")

我已经能够加载整个“数据集”。

我只想在我的 json 文件中加载question键(所以所有问题,没有question_idimage_filename

我尝试使用train_data.get("question")但我得到错误list object has no attribute 'get'

我也尝试使用这里建议的内容: 嵌套字典没有任何成功

那么,回到我的问题,我如何只选择question字段?

尝试这个:

train_data_jsonload = json.load(f)
for questions_info in train_data_jsonload["questions"]:
   question  = question_info["question"]
   #Do required stuff here if needed.

如果您希望将问题列表作为输出,只需使用列表理解:

text_questions = [item["question"] for item in train_data_jsonload.get("questions")]

最后我找到了解决方案:

for element in train_data:
    print(element.get("question"))

将打印每个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM