【发布时间】:2018-03-02 01:25:43
【问题描述】:
我目前有一个格式如下的 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"
}
}
]
【问题讨论】:
-
你能更好地了解结构吗?我需要知道您需要哪个键以及列表最后应该是什么样子。还有很多文件结果吗?是你想要的标签列表吗?
-
@cal97g 是的,我必须收集文件名、所有标签和置信度值。解析单个 JSON 对象应该没有任何问题,我之前从未在 Python 中处理/导入过这么大的嵌套 JSON 文件。
-
问题不是很清楚。你能提供预期的结果吗?此外,如果您提供您所做尝试的代码以便更好地了解您所追求的目标,这将很有用
-
@damores 基本上问题是我想将 JSON 文件导入到我的 python 脚本中。假设我使用 cal97g (top big_json_file sn-p) 提供的最佳答案,我将收到 ValueError: No JSON object could be decoded because of the format of the JSON file
标签: python json python-3.x parsing