简体   繁体   English

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

[英]Saving JSON File with Many Nested Objects to a List

I currently have a JSON file that has the following format.我目前有一个具有以下格式的 JSON 文件。 Keep in mind that this is not the entire file.请记住,这不是整个文件。 The entire JSON file is comprised of hundreds of lists of key, value pairs that follow one after another between square brackets [][][]...etc.整个 JSON 文件由数百个键值对列表组成,这些键值对在方括号 [][][]... 之间一个接一个。 I am trying to store each of these individual lists eg the list below in a structure so that I may iterate the structure and parse each list for filename, labels etc. I initially tried to import this using json.loads() but I am having issues due the nested nature of the objects.我试图将这些单独的列表中的每一个存储在一个结构中,例如下面的列表,以便我可以迭代结构并解析每个列表的文件名、标签等。我最初尝试使用 json.loads() 导入它,但我有由于对象的嵌套性质而导致的问题。 I would appreciate any ideas/insight on how I can go about importing this file into a list or other appropriate python structure.我将不胜感激有关如何将此文件导入列表或其他适当的 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'])

Or if you want to store the file name and the list I'd recommend something like:或者,如果您想存储文件名和列表,我建议您使用以下内容:

my_processed_dict = {}
for file in big_json_file:
    my_processed_dict[file['File_Name']] = file['Analysis']['Labels']

where you can iterate over my_processed_dict with:您可以在其中迭代 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM