Python 将包含许多嵌套对象的JSON文件保存到列表中

Python 将包含许多嵌套对象的JSON文件保存到列表中,python,json,python-3.x,parsing,Python,Json,Python 3.x,Parsing,我目前有一个JSON文件,其格式如下。请记住,这不是整个文件。整个JSON文件由数百个键、值对列表组成,这些键、值对在方括号[]等之间依次排列。我正在尝试将这些单独的列表(例如,下面的列表)存储在一个结构中,以便我可以迭代该结构并解析每个列表的文件名、标签等。我最初尝试使用json.loads()导入此列表,但由于对象的嵌套性质,我遇到了问题。对于如何将此文件导入列表或其他适当的python结构,如果有任何想法/见解,我将不胜感激 [ { "File_Name": "1.jpg

我目前有一个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"
      }
   }
]
或者,如果您想存储文件名和列表,我建议您使用以下方式:

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

你能更好地了解这个结构吗?我需要知道你需要哪把钥匙,以及最后的列表应该是什么样子。还有很多文件结果吗?这是您想要的标签列表吗?@cal97g是的,我必须收集文件名、所有标签和置信值。解析单个JSON对象应该不会有任何问题,我只是从来没有在Python中处理/导入过这么大的嵌套JSON文件。问题还不是很清楚。你能提供预期的结果吗?此外,如果您提供您所做尝试的代码,以便更好地了解您正在做的事情,这将非常有用after@damores基本上,问题是我想将一个JSON文件导入到我的python脚本中。假设我使用cal97g(顶级大json文件片段)提供的最佳答案,我将收到一个ValueError:由于json文件的格式,无法解码任何json对象
for key, value in my_processed_dict.items():
    # value is the list of confidence values!
    pass