【问题标题】:Print JSON Data with python where there is no key & value在没有键和值的情况下使用 python 打印 JSON 数据
【发布时间】:2020-04-29 08:41:56
【问题描述】:

我目前正在处理一些具有挑战性的 JSON 数据,我需要在我的脚本中的延迟浮动旁边打印设备名称,我可以轻松打印延迟浮动,因为有一个 key:value ,但是设备名称不一样,因此我无法弄清楚如何打印它,尤其是当它为我循环通过以检索数据的每个 API Url 发生变化时

我要打印的数据是"DEVICE123-Et10"

请参阅下面的 JSON 数据,

{
  "notifications": [
    {
      "timestamp": "511513234234",
      "path_elements": [
        "Devices",
        "DEVICE1",
        "versioned-data",
        "connectivityMonitor",
        "status",
        "hostStatus",
        "DEVICE123-Et10",
        "defaultStats"
      ],
      "updates": {
        "httpResponseTime": {
          "key": "httpResponseTime",
          "value": {
            "float": 0
          }
        }
      }
    },
    {
      "timestamp": "15153324243",
      "path_elements": [
        "Devices",
        "DEVICE1",
        "versioned-data",
        "connectivityMonitor",
        "status",
        "hostStatus",
        "DEVICE123-Et10",
        "defaultStats"
      ],
      "updates": {
        "packetLoss": {
          "key": "packetLoss",
          "value": {
            "int": 0
          }
        }
      }
    },
    {
      "timestamp": "151522324234",
      "path_elements": [
        "Devices",
        "DEVICE1",
        "versioned-data",
        "connectivityMonitor",
        "status",
        "hostStatus",
        "DEVICE123-Et10",
        "defaultStats"
      ],
      "updates": {
        "latency": {
          "key": "latency",
          "value": {
            "float": 0.238756565643454
          }
        }
      }
    },
    {
      "timestamp": "158056745645645",
      "path_elements": [
        "Devices",
        "DEVICE1",
        "versioned-data",
        "connectivityMonitor",
        "status",
        "hostStatus",
        "DEVICE123-Et10",
        "defaultStats"
      ],
      "updates": {
        "jitter": {
          "key": "jitter",
          "value": {
            "float": 0.03500000213213
          }
        }
      }
    }
  ]
}

我用来循环浏览我的 URL 列表并获取延迟的当前代码:

     jsonrequest = requests.get(url, cookies=cookies, verify=False).json()
     try:
         print(jsonrequest['notifications'][2]['updates']['latency']['value']['float'])
     except KeyError:
         print(jsonrequest['notifications'][1]['updates']['latency']['value']['float'])```

【问题讨论】:

  • 由于设备名称在列表中总是倒数第二个,因此只需使用键“path_elements”访问列表,然后获取倒数第二个元素。类似json_data["path_elements"][-2]

标签: python json python-3.x api python-requests


【解决方案1】:

我继续写了一个脚本来做你想做的事。它循环遍历所有通知,直到找到“延迟”更新。然后它从列表中取出倒数第二个项目,因为它总是倒数第二个。

import json
import requests

data = requests.get(url, cookies=cookies, verify=False).json()
notifications = data["notifications"]
for notification in notifications:
    if notification["updates"].get("latency"):
        latency = notification["updates"]["latency"]["value"]["float"]
        name = notification["path_elements"][-2]
        print(name, latency)

【讨论】:

  • 上面的例子似乎不起作用,我当前没有设备名称的代码如下所示:` jsonrequest = requests.get(url, cookies=cookies, verify=False).json( ) 尝试: print(jsonrequest['notifications'][2]['updates']['latency']['value']['float']) 除了 KeyError: print(jsonrequest['notifications'][1][ 'updates']['latency']['value']['float'])`
  • 当你说它不起作用时,错误是什么?它有什么问题?
  • 也许我使用了不正确的语法,但是我不确定如何使用您的代码单独遍历每个 url,因为我的 JSON 数据来自 URL 列表而不是特定文件。因此我已经在使用for url in URLs:
  • 它应该仍然有效。我已经更新它以使其更清晰。我的解决方案有什么错误?
  • TypeError: JSON 对象必须是 str、bytes 或 bytearray,而不是 dict
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 2011-12-09
  • 2021-12-22
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多