浏览 JSON 响应以捕获值

发布于 2025-01-20 12:44:18 字数 767 浏览 3 评论 0原文

我试图在“标签”键下的“ 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"
      }

I am trying to capture the "ilapd" string in the "process:ilapd" value under the "Tags" key but have not been successful. How do I go about grabbing this string?

I have tried to iterate through the data with several variables of a for loop but keep getting errors for type integers.

JSON data below:

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"
      }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘曦 2025-01-27 12:44:18

您可以使用 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))

You can use 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)

Prints:

ilapd

Or using re module:

import re

r = re.compile(r"process:(.*)")

for t in data["Tags"].split(","):
    if (m := r.search(t)) :
        print(m.group(1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文