使用 Python 从 JSON 文件的列表中的目录中的键中读取值

Reading values from a key within a Directory within a List from a JSON file using Python

我有一个 ML 丑陋的 JSON 文件,我正试图从中提取值。

下面是列表中的第一个条目: [ {"ID":"ckzjqmtvf3ltd0z7yhogt3ttm", "DataRow ID":"ckzjq9ry03hfp0zux8xwkhkvv", "Label":{"objects": [{"featureId":"ckzjqnrn600012469dfbkfr1q", "schemaId":"ckzjqkttm3m8v0z78e7rdbkda", "color":"#1CE6FF", "title":"Weed", "value":"weed", "bbox":{"top":1003, "left":810, "height":848, "width":881}, {"featureId":"ckzjqo6iy000724690onwpxax", "schemaId":"ckzjqkttm3m8v0z78e7rdbkda", "color":"#1CE6FF", "title":"Weed", "value":"weed", "bbox"{"top":1780, "left":410, "height":264, "width":254}, {"featureId":"ckzjqobrv000a24697tc84nrr", "schemaId":"ckzjqkttm3m8v0z78e7rdbkda", "color":"#1CE6FF", "title":"Weed", "value":"weed", "bbox":{"top":1060, "left":1799, "height":471, "width":523},]

我正在尝试提取标题和 bbox 信息。

我试过数据["标签"][0]["标题"],但我得到:

KeyError: 0

任何帮助都会很棒!

问题出在数据本身,因为json没有正确关闭。我不得不删除“,”并添加“]”。之后data.json有效。

import json

with open("data.json") as file:
    data = json.load(file)

    objects = data[0]["Label"]["objects"]
    for object in objects:
        title = object["title"]
        bbox = object["bbox"]
        print(title)
        print(bbox)

读取 json 文件中所有项目的标题和 bbox 信息的代码:

import json

with open("data.json") as file:
    data = json.load(file)

for item in data:
    objects = item['Label']['objects']
    for obj in objects:
        title = obj['title']
        bbox = obj['bbox']