json - 读取json并访问多个key

标签 json python-3.x

我正在尝试通过 python 中的循环和条件语句来获取 json 文件的内容。

这是 JSON 文件内容:

{
    "count": 3,
    "result": [
        {
            "type": "first",
            "first": {
                "_links": {
                    "self1": {
                        "href": "/c6a5c1db-cf6e-4b12-9944-a1b6451963d4"
                    },
                    "self2": {
                        "href": "https://example.com"
                    },
                    "ver": {
                        "href": "https://example1.com"
                    }
                },
                "id": "c6a5c1db-cf6e-4b12-9944-a1b6451963d4",
                "tags": []
            }
        },
        {
            "type": "year",
            "year": {
                "month": {
                    "api1": {
                        "href": "https://Ap1.com"
                    },
                    "api2": {
                        "href": "FETCH-CONTENT"
                    },
                    "api3": {
                        "href": "https://Ap3.com"
                    },
                    "api4": {
                        "href": "https://Ap4.com"
                    }
                },
                "idProvider": {
                    "id": "sfsmkfmskf",
                    "name": "Apikey"
                },
                "tags": []
            }
        },
        {
            "type": "year",
            "year": {
                "month": {
                    "api1": {
                        "href": "https://Ap11.com"
                    },
                    "api2": {
                        "href": "FETCH-CONTENT-1"
                    },
                    "api3": {
                        "href": "https://Ap13.com"
                    },
                    "api4": {
                        "href": "https://Ap14.com"
                    }
                },
                "identityProvider": {
                    "id": "00000000-0000-0000-0000-000000000000",
                    "name": ""
                },
                "tags": []
            }
        }
    ]
}

我正在尝试从第一个对象访问 self2 href 和从第二个对象访问 api3 href_1和第三个物体。如果没有第一个对象,我可以访问 api3 href_1 并且它会打印所需的键。

但是,当我通过完整文件打印内容时,无法打印内容。因为它没有为所有对象获得相同的键名称。

打印

print([item['year']['month']['api3'] for item in data['result']])

如果 json 文件不包含结果数组的第一个元素,上面的行将打印所需的数据。当我添加结果数组的第一个元素时,它没有给出任何结果。

最佳答案

使用以下语句忽略具有 "type": "first" 的项目。有用。

print([item['year']['month']['api3'] for item in data['result'] if "year"in item])

您可以在下面的示例中看到它。

>>> data = {
... "count": 3,
... "result": [
...     {
...         "type": "first",
...         "first": {
...             "_links": {
...                 "self1": {
...                     "href": "/c6a5c1db-cf6e-4b12-9944-a1b6451963d4"
...                 },
...                 "self2": {
...                     "href": "https://example.com"
...                 },
...                 "ver": {
...                     "href": "https://example1.com"
...                 }
...             },
...             "id": "c6a5c1db-cf6e-4b12-9944-a1b6451963d4",
...             "tags": []
...         }
...     },
...     {
...         "type": "year",
...         "year": {
...             "month": {
...                 "api1": {
...                     "href": "https://Ap1.com"
...                 },
...                 "api2": {
...                     "href": "FETCH-CONTENT"
...                 },
...                 "api3": {
...                     "href": "https://Ap3.com"
...                 },
...                 "api4": {
...                     "href": "https://Ap4.com"
...                 }
...             },
...             "idProvider": {
...                 "id": "sfsmkfmskf",
...                 "name": "Apikey"
...             },
...             "tags": []
...         }
...     },
...     {
...         "type": "year",
...         "year": {
...             "month": {
...                 "api1": {
...                     "href": "https://Ap11.com"
...                 },
...                 "api2": {
...                     "href": "FETCH-CONTENT-1"
...                 },
...                 "api3": {
...                     "href": "https://Ap13.com"
...                 },
...                 "api4": {
...                     "href": "https://Ap14.com"
...                 }
...             },
...             "identityProvider": {
...                 "id": "00000000-0000-0000-0000-000000000000",
...                 "name": ""
...             },
...             "tags": []
...         }
...     }
... ]}
>>> 
>>> print([item['year']['month']['api3'] for item in data['result'] if "year" in item])
[{'href': 'https://Ap3.com'}, {'href': 'https://Ap13.com'}]
>>> 

关于json - 读取json并访问多个key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56149717/

相关文章:

json - MSON 中带有对象的嵌套数组在 Apiary.io 文档中为空

macos - 列出 Mac OS X 和 Python 3 中的串行端口

Python3-在函数内调用 exec(open().read()) 的问题

python : How to prompt user to enter numbers to an empty list?

javascript - 如何根据子属性将 JSON 排序到数组中

arrays - Swift 数组不会填充 JSON 数据

asp.net - 为什么此JSON返回为 "Invalid JSON primitive"?

javascript - 如何在传单 map 上显示SQL查询

python - 数据框 fit_transform 抛出错误,看似不正确的错误

python - 使用 fitEllipse 对图像进行椭圆拟合