【问题标题】:Python: Getting key error in though key exist, Azure APIPython:虽然存在密钥,但在 Azure API 中出现密钥错误
【发布时间】:2020-07-23 06:02:00
【问题描述】:

我正在尝试使用 Azure API 并获取资源。响应是一个很大的 JSON。

{
...
{
    "id": "/subscriptions/<subscription_name>/resourceGroups/<resouce_group_name>/providers/Microsoft.Compute/virtualMachines/<VM_name>",
    "name": "<VM_name>",
    "type": "Microsoft.Compute/virtualMachines",
    "location": "eastus",
    "tags": {}
},
...
}

我以修改后的形式将响应保存在字典中(在名为 self.resources 的变量中,我正在使用类),其中键是 id,值是 {'name': response['name'], 'type': response['type']}

类似的,

{
  '/subscriptions/<subscription_name>/resourceGroups/<resouce_group_name>/providers/Microsoft.Compute/virtualMachines/<VM_name>': 
                 {
            'name': response['name'],
            'type': response['type']
                 }
}

但是,当我尝试以另一种方法访问 self.resources 中的一个键时,它给了我 1345 个中有 528 个键的键错误。

我可以确认密钥存在,因为我可以在获取响应后写入的 JSON 文件中看到它。

我什至尝试从该 JSON 文件中读取而不是使用 self.resources,但错误仍然存​​在。

这可能是什么原因? 我正在使用 python 3.8,Windows 10。

【问题讨论】:

    标签: python json string rest azure-rest-api


    【解决方案1】:

    所以我解决了这个问题,这是一个非常愚蠢的问题,实际上我使用了两个 API,正如我所说,一个是获取资源,另一个 API 用于警报,两者都有资源 ID,但在警报中一个虚拟机名称大写。

    我确认了,因为我没有逐字比较,只是使用VS code find选项进行搜索,并且不区分大小写。

    我希望它可以解决任何为 ID 苦苦挣扎的人的问题。

    【讨论】:

      猜你喜欢
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 2017-10-01
      相关资源
      最近更新 更多