-1

我正在使用 PRTG HTTP API 来提取传感器信息,我的目标是过滤具有此值的传感器

<status>Down</status>

到目前为止,我已经阅读了 Stack 和 Google 搜索中的很多参考资料,但我无法应用这些解决方案,所以粘贴我尝试过的所有代码都是徒劳的,但对我来说更有意义的是这个一

JSON的结构是这样的

{
  "prtg-version": "19.1.49.1966",
  "treesize": 17701,
  "status%3Ddown": [
    {
      "group": "SOME GROUP",
      "device": "SOME SWITCH",
      "sensor": "Uptime",
      "status": "Down",
      "status_raw": 5
    },
    {
      "group": "SOME GROUP",
      "device": "SOME SWITCH",
      "sensor": "System Health Memory",
      "status": "Up",
      "status_raw": 3
    },
  ]
}

现在这里是代码

import json
import requests
url = "https://prtg.c3ntro.com/api/table.jsoncontent=status=down&username=usr&passhash=hash&count=100"
response = requests.get(url)
data = response.json()

d = data
result_dict = [d for d in data['status%3Ddown'] if d['status'] == 'Down']
print(result_dict)


#Code has been fixed and now it works

现在只打印出 Down 状态传感器

4

2 回答 2

2

列表理解应该起作用:

[d for d in data['status%3Ddown'] if d['status'] == 'Down']

"status%3Ddown"返回列表中包含的所有条目"status": "Down"

[{'device': 'SOME SWITCH',
  'group': 'SOME GROUP',
  'sensor': 'Uptime',
  'status': 'Down',
  'status_raw': 5}]
于 2019-05-07T18:48:37.570 回答
0
d = data["prtg-version", "treesize", "status%3Ddown"]

那行代码正在尝试使用文字三部分 key 访问元素"prtg-version", "treesize", "status%3Ddown",这当然不存在。

看起来你只想要这个:

d = data["status%3Ddown"]
于 2019-05-07T18:45:29.297 回答