[英]Saving JSON File with Many Nested Objects to a List
我目前有一個具有以下格式的 JSON 文件。 請記住,這不是整個文件。 整個 JSON 文件由數百個鍵值對列表組成,這些鍵值對在方括號 [][][]... 之間一個接一個。 我試圖將這些單獨的列表中的每一個存儲在一個結構中,例如下面的列表,以便我可以迭代結構並解析每個列表的文件名、標簽等。我最初嘗試使用 json.loads() 導入它,但我有由於對象的嵌套性質而導致的問題。 我將不勝感激有關如何將此文件導入列表或其他適當的 Python 結構的任何想法/見解。
[
{
"File_Name": "1.jpg",
"Analysis": {
"Labels": [
{
"Confidence": 94.77251434326172,
"Name": "Flora"
},
{
"Confidence": 94.77251434326172,
"Name": "Grass"
},
{
"Confidence": 94.77251434326172,
"Name": "Plant"
},
{
"Confidence": 78.49254608154297,
"Name": "Animal"
},
{
"Confidence": 78.49254608154297,
"Name": "Cheetah"
},
{
"Confidence": 78.49254608154297,
"Name": "Mammal"
},
{
"Confidence": 78.49254608154297,
"Name": "Wildlife"
},
{
"Confidence": 69.79740142822266,
"Name": "Field"
},
{
"Confidence": 69.79740142822266,
"Name": "Grassland"
},
{
"Confidence": 69.79740142822266,
"Name": "Outdoors"
},
{
"Confidence": 67.31356048583984,
"Name": "Leisure Activities"
},
{
"Confidence": 67.31356048583984,
"Name": "Walking"
},
{
"Confidence": 57.44683837890625,
"Name": "Jaguar"
},
{
"Confidence": 57.44683837890625,
"Name": "Leopard"
},
{
"Confidence": 57.44683837890625,
"Name": "Panther"
},
{
"Confidence": 55.88261032104492,
"Name": "Bush"
},
{
"Confidence": 55.88261032104492,
"Name": "Vegetation"
},
{
"Confidence": 53.4413948059082,
"Name": "Lawn"
}
],
"ResponseMetadata": {
"RetryAttempts": 0,
"HTTPStatusCode": 200,
"RequestId": "978e32e4-1da8-11e8-a380-cd680f89684e",
"HTTPHeaders": {
"date": "Thu, 01 Mar 2018 23:30:59 GMT",
"x-amzn-requestid": "978e32e4-1da8-11e8-a380-cd680f89684e",
"content-length": "947",
"content-type": "application/x-amz-json-1.1",
"connection": "keep-alive"
}
},
"OrientationCorrection": "ROTATE_0"
}
}
][
{
"File_Name": "2.jpg",
"Analysis": {
"Labels": [
{
"Confidence": 98.57389068603516,
"Name": "Astronomy"
},
{
"Confidence": 98.57389068603516,
"Name": "Galaxy"
},
{
"Confidence": 98.57389068603516,
"Name": "Nebula"
},
{
"Confidence": 98.57389068603516,
"Name": "Night"
},
{
"Confidence": 98.57389068603516,
"Name": "Outdoors"
},
{
"Confidence": 98.57389068603516,
"Name": "Outer Space"
},
{
"Confidence": 98.57389068603516,
"Name": "Space"
},
{
"Confidence": 98.57389068603516,
"Name": "Universe"
}
],
"ResponseMetadata": {
"RetryAttempts": 0,
"HTTPStatusCode": 200,
"RequestId": "98d2c109-1da8-11e8-a2d9-b91cf22c7f33",
"HTTPHeaders": {
"date": "Thu, 01 Mar 2018 23:30:59 GMT",
"x-amzn-requestid": "98d2c109-1da8-11e8-a2d9-b91cf22c7f33",
"content-length": "449",
"content-type": "application/x-amz-json-1.1",
"connection": "keep-alive"
}
},
"OrientationCorrection": "ROTATE_0"
}
},
{
"File_Name": "2.jpg",
"Analysis": {
"Labels": [
{
"Confidence": 98.57389068603516,
"Name": "Astronomy"
},
{
"Confidence": 98.57389068603516,
"Name": "Galaxy"
},
{
"Confidence": 98.57389068603516,
"Name": "Nebula"
},
{
"Confidence": 98.57389068603516,
"Name": "Night"
},
{
"Confidence": 98.57389068603516,
"Name": "Outdoors"
},
{
"Confidence": 98.57389068603516,
"Name": "Outer Space"
},
{
"Confidence": 98.57389068603516,
"Name": "Space"
},
{
"Confidence": 98.57389068603516,
"Name": "Universe"
}
],
"ResponseMetadata": {
"RetryAttempts": 0,
"HTTPStatusCode": 200,
"RequestId": "98d2c109-1da8-11e8-a2d9-b91cf22c7f33",
"HTTPHeaders": {
"date": "Thu, 01 Mar 2018 23:30:59 GMT",
"x-amzn-requestid": "98d2c109-1da8-11e8-a2d9-b91cf22c7f33",
"content-length": "449",
"content-type": "application/x-amz-json-1.1",
"connection": "keep-alive"
}
},
"OrientationCorrection": "ROTATE_0"
}
}
]
big_json_file = json.loads(file_string)
big_list_of_labels = []
for file in big_json_file:
big_list_of_labels.append(file['Analysis']['Labels'])
或者,如果您想存儲文件名和列表,我建議您使用以下內容:
my_processed_dict = {}
for file in big_json_file:
my_processed_dict[file['File_Name']] = file['Analysis']['Labels']
您可以在其中迭代 my_processed_dict :
for key, value in my_processed_dict.items():
# value is the list of confidence values!
pass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.