将包含许多嵌套对象的JSON文件保存到Lis

2024-04-20 10:43:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前有一个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"
      }
   }
]

Tags: 文件namejson列表labelsspaceanalysiscontent
1条回答
网友
1楼 · 发布于 2024-04-20 10:43:32
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']

在这里,您可以使用以下命令迭代我的\u处理的\u dict:

for key, value in my_processed_dict.items():
    # value is the list of confidence values!
    pass

相关问题 更多 >