将包含许多嵌套对象的JSON文件保存到列表中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将包含许多嵌套对象的JSON文件保存到列表中相关的知识,希望对你有一定的参考价值。
我目前有一个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
以上是关于将包含许多嵌套对象的JSON文件保存到列表中的主要内容,如果未能解决你的问题,请参考以下文章
如何正确地将 JSON 字符串反序列化为包含另一个类的嵌套列表的类