浏览 JSON 响应以捕获值
Navigating through a JSON Response to capture a value
我试图在“Tags”键下的“process:ilapd”值中捕获“ilapd”字符串,但没有成功。我该如何获取这个字符串?
我尝试使用 for 循环的多个变量遍历数据,但一直收到整数类型的错误。
JSON 数据如下:
data = {
"alertOwner":"team",
"assignGroup":"team",
"component":"lnx2",
"Tags":"application:unknown, appowner:secops, bgs:performance, businessgroup:top, drexercise:no, env:nonprod, facility:hq, host:lnx2, location:somewhere, manager:smith, monitor, monitoring24x7:yes, osowner:unix, process:ilapd",
"description":"Process ilapd is not running on lnx2, expected state is running,",
"Event Url":"https://app.datadoghq.com/monitors#67856691",
"logicalName":"lnx2",
"Metric Graph":"<img src=\"\" />",
"pageGroups":"team",
"priority":"4",
"Snapshot Link":"",
"type":"test"
}
您可以使用 str.split
+ str.startswith
:
data = {
"alertOwner": "team",
"assignGroup": "team",
"component": "lnx2",
"Tags": "application:unknown, appowner:secops, bgs:performance, businessgroup:top, drexercise:no, env:nonprod, facility:hq, host:lnx2, location:somewhere, manager:smith, monitor, monitoring24x7:yes, osowner:unix, process:ilapd",
"description": "Process ilapd is not running on lnx2, expected state is running,",
"Event Url": "https://app.datadoghq.com/monitors#67856691",
"logicalName": "lnx2",
"Metric Graph": '<img src="" />',
"pageGroups": "team",
"priority": "4",
"Snapshot Link": "",
"type": "test",
}
process = next(
tag.split(":")[-1]
for tag in map(str.strip, data["Tags"].split(","))
if tag.startswith("process:")
)
print(process)
打印:
ilapd
或使用re
模块:
import re
r = re.compile(r"process:(.*)")
for t in data["Tags"].split(","):
if (m := r.search(t)) :
print(m.group(1))
我试图在“Tags”键下的“process:ilapd”值中捕获“ilapd”字符串,但没有成功。我该如何获取这个字符串?
我尝试使用 for 循环的多个变量遍历数据,但一直收到整数类型的错误。
JSON 数据如下:
data = {
"alertOwner":"team",
"assignGroup":"team",
"component":"lnx2",
"Tags":"application:unknown, appowner:secops, bgs:performance, businessgroup:top, drexercise:no, env:nonprod, facility:hq, host:lnx2, location:somewhere, manager:smith, monitor, monitoring24x7:yes, osowner:unix, process:ilapd",
"description":"Process ilapd is not running on lnx2, expected state is running,",
"Event Url":"https://app.datadoghq.com/monitors#67856691",
"logicalName":"lnx2",
"Metric Graph":"<img src=\"\" />",
"pageGroups":"team",
"priority":"4",
"Snapshot Link":"",
"type":"test"
}
您可以使用 str.split
+ str.startswith
:
data = {
"alertOwner": "team",
"assignGroup": "team",
"component": "lnx2",
"Tags": "application:unknown, appowner:secops, bgs:performance, businessgroup:top, drexercise:no, env:nonprod, facility:hq, host:lnx2, location:somewhere, manager:smith, monitor, monitoring24x7:yes, osowner:unix, process:ilapd",
"description": "Process ilapd is not running on lnx2, expected state is running,",
"Event Url": "https://app.datadoghq.com/monitors#67856691",
"logicalName": "lnx2",
"Metric Graph": '<img src="" />',
"pageGroups": "team",
"priority": "4",
"Snapshot Link": "",
"type": "test",
}
process = next(
tag.split(":")[-1]
for tag in map(str.strip, data["Tags"].split(","))
if tag.startswith("process:")
)
print(process)
打印:
ilapd
或使用re
模块:
import re
r = re.compile(r"process:(.*)")
for t in data["Tags"].split(","):
if (m := r.search(t)) :
print(m.group(1))