我有一个嵌套的 json ,如下所示。我正在尝试从下面的 json 中获取城市和标签的值。
{
"5c7a3dd5-29e4-4f5b-9dc8-d2d451906cf1": {
"id": "1d14eba1-de9e-4e53-9e9e-619432fd815b",
"opinion_id": "3063e6ff-1483-4daf-8e80-41afa43b6348",
"paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
"position": 7,
"city": "Berlin.",
"label": "Capital"
},
"0c0adcad-992f-4c30-aa58-7bf539f8d649": {
"case_id": "6f6e70f7-3257-4f26-a8fa-8cf0e5f97920",
"opinion_id": "71b435ff-112c-470b-a36b-c552e54a5605",
"paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
"position": 3,
"city": "Frankfurt.",
"label": "Economic Capital"
},
"de968cdf-d865-4049-9f7d-f9a68be27432": {
"case_id": "2d749a58-412c-4bc8-b268-7d5f7bec3d9a",
"opinion_id": "1836c972-d6f7-420b-ba33-8f863cfca482",
"paragraph_id": "e6d419fe-5017-47f6-acf1-0e74034aed4a",
"position": 4,
"city": "Paris.",
"label": "Capital"
}
}
我尝试的是循环遍历 json 并将值提取到字典列表中。它给我带来了一个关键错误。我有点明白了。如果外部json的键相同,那么我可以使用text = jsonData['KEY'][i]["city"]
with open(json_file_name) as input:
jsonData = json.load(input)
JSONLength = len(jsonData)
data = []
classes = dict()
for i in range(0, JSONLength):
text = jsonData[i]["city"]
label = jsonData[i]["label"]
print(text)
print(label)
data += [(classes[label], text)]
开心每一天1111
青春有我
慕莱坞森
相关分类