遍历嵌套 JSON

发布于 2025-01-14 01:06:50 字数 1201 浏览 0 评论 0原文

我有以下字典数据。

"tags": {
    "tags":
    [
        {
            "applied_at": 1645181955,
            "applied_by":
            {
                "id": "1288541",
                "type": "admin"
            },
            "id": "6355625",
            "name": "No Reply > 2hrs",
            "type": "tag"
        },
        {
            "applied_at": 1645185249,
            "applied_by":
            {
                "id": "4597637",
                "type": "admin"
            },
            "id": "6258671",
            "name": "Resolution Provided",
            "type": "tag"
        },
        {
            "applied_at": 1647238491,
            "applied_by":
            {
                "id": "4597637",
                "type": "admin"
            },
            "id": "6258671",
            "name": "Resolution Provided",
            "type": "tag"
        }
    ],
    "type": "tag.list"
}

正如您从上面看到的,我们有一个名为 Resolution Provided 的标签。

我的条件是获得第一个分辨率应用时间和第二个分辨率应用时间,并且可能会持续n次。您可以从名为 applied_at 的同一组字段获取时间。

我正在尝试通过 Python 本身来实现这一点。

有人可以帮助我采取一种方法吗,我不是开发人员,但试图成为一名开发人员。所以任何有建设性的批评我都可以接受。我所需要的只是一个逻辑,这样我就可以尝试它。

I have the following dictionary data.

"tags": {
    "tags":
    [
        {
            "applied_at": 1645181955,
            "applied_by":
            {
                "id": "1288541",
                "type": "admin"
            },
            "id": "6355625",
            "name": "No Reply > 2hrs",
            "type": "tag"
        },
        {
            "applied_at": 1645185249,
            "applied_by":
            {
                "id": "4597637",
                "type": "admin"
            },
            "id": "6258671",
            "name": "Resolution Provided",
            "type": "tag"
        },
        {
            "applied_at": 1647238491,
            "applied_by":
            {
                "id": "4597637",
                "type": "admin"
            },
            "id": "6258671",
            "name": "Resolution Provided",
            "type": "tag"
        }
    ],
    "type": "tag.list"
}

As you see from the above, we have a tag called Resolution Provided.

My condition is to get the first resolution applied time and second resolution applied time and it may go on and on like n times. The time you can get from the same group field named as applied_at.

I am trying to achieve this via Python itself.

Can someone help me with an approach, I am not developer but trying to be one. So any constructive critics are fine with me. All I need is a logic so that I can try it.

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

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

发布评论

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

评论(3

甜警司 2025-01-21 01:06:50

正如我在评论中所说,首先你想将 json 字符串解析为字典

import json

a = """
{
"tags": {
    "tags":
    [
        {
            "applied_at": 1645181955,
            "applied_by":
            {
                "id": "1288541",
                "type": "admin"
            },
            "id": "6355625",
            "name": "No Reply > 2hrs",
            "type": "tag"
        },
        { 
            "applied_at": 1645185249,
            "applied_by":
            {
                "id": "4597637",
                "type": "admin"
            },
            "id": "6258671",
            "name": "Resolution Provided",
            "type": "tag"
        },
        { 
            "applied_at": 1647238491,
            "applied_by":
            {
                "id": "4597637",
                "type": "admin"
            },
            "id": "6258671",
            "name": "Resolution Provided",
            "type": "tag"
        }
    ],
    "type": "tag.list"
}
}
"""

parsed = json.loads(a)
print(parsed)

输出:

{'tags': {'tags': [{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}, {'applied_at': 1645185249, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}, {'applied_at': 1647238491, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}], 'type': 'tag.list'}}

然后你想按它的 applied_at 标签进行排序:

tags = parsed["tags"]["tags"]
tags.sort(key=lambda i: i["applied_at"])
print(tags)

输出:

[{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}, {'applied_at': 1645185249, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}, {'applied_at': 1647238491, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}]

两个输出是相同的,因为你的初始 json 文件已经排序。

然后,如果您想知道第一个应用哪个标签,只需调用 tags[0],第二个应用 tags[1],依此类推

print 的输出(标签[0])

{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}

As I said in comment, first you want to parse the json string into dictionary

import json

a = """
{
"tags": {
    "tags":
    [
        {
            "applied_at": 1645181955,
            "applied_by":
            {
                "id": "1288541",
                "type": "admin"
            },
            "id": "6355625",
            "name": "No Reply > 2hrs",
            "type": "tag"
        },
        { 
            "applied_at": 1645185249,
            "applied_by":
            {
                "id": "4597637",
                "type": "admin"
            },
            "id": "6258671",
            "name": "Resolution Provided",
            "type": "tag"
        },
        { 
            "applied_at": 1647238491,
            "applied_by":
            {
                "id": "4597637",
                "type": "admin"
            },
            "id": "6258671",
            "name": "Resolution Provided",
            "type": "tag"
        }
    ],
    "type": "tag.list"
}
}
"""

parsed = json.loads(a)
print(parsed)

Outputs:

{'tags': {'tags': [{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}, {'applied_at': 1645185249, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}, {'applied_at': 1647238491, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}], 'type': 'tag.list'}}

Then you want to sort by it's applied_at tag:

tags = parsed["tags"]["tags"]
tags.sort(key=lambda i: i["applied_at"])
print(tags)

outputs:

[{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}, {'applied_at': 1645185249, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}, {'applied_at': 1647238491, 'applied_by': {'id': '4597637', 'type': 'admin'}, 'id': '6258671', 'name': 'Resolution Provided', 'type': 'tag'}]

The two outputs is identical because your initial json file is already sorted.

Then if you want to know which tag is first applied, just call tags[0], second one by tags[1], and so on

output of print(tags[0])

{'applied_at': 1645181955, 'applied_by': {'id': '1288541', 'type': 'admin'}, 'id': '6355625', 'name': 'No Reply > 2hrs', 'type': 'tag'}
李不 2025-01-21 01:06:50

我不明白你到底想做什么。我从您的描述中得到的是,名称为“Resolution Provided”的标签对您很重要,对于这些标签,您希望使用“applied_at”字段执行某些操作。

请注意,您提供的“JSON”实际上不是有效的 JSON。您需要在该数据周围添加一组额外的花括号,以使其成为有效的 JSON。

以下是如何提取名称为“Resolution Provided”的标签的“applied_at”字段。此代码添加了额外的花括号,以允许您的数据被解析为有效的 JSON:

import json

data = json.loads("{" + data + "}")

for tag in data['tags']['tags']:
    if tag['name'] == "Resolution Provided":
        print(tag['applied_at'])

结果:

1645185249
1647238491

I don't understand exactly what you want to do. What I get from your description is that tags with the name "Resolution Provided" are important to you, and for those tags, you want to do something with the "applied_at" field.

Note that the "JSON" you provide is actually not valid JSON. You need an extra set of curly braces around that data to make it valid JSON.

Here's how to extract the "applied_at" fields for tags that have the name "Resolution Provided". This code adds the extra curly braces to allow your data to be parsed as valid JSON:

import json

data = json.loads("{" + data + "}")

for tag in data['tags']['tags']:
    if tag['name'] == "Resolution Provided":
        print(tag['applied_at'])

Result:

1645185249
1647238491
梦初启 2025-01-21 01:06:50

我能想到的第一个方法是先到达“标签”,然后我会看到一个字典列表。所以我可以在这里使用 for 循环来遍历这些字典。
然后我将轻松触及按键,并用这些值做我想做的事情。

json_data["tags"]["tags"]

带您到要使用 for 循环迭代的字典列表。

for current_dict in json_data["tags"]["tags"]:
    if current_dict["name"] == "Resolution Provided":
        print(current_dict["applied_at"])

The first that i can come up with is first reach the "tags" and I after that I'll see a list of dicts. So I can use a for loop in here to traverse on those dicts.
And then I'll reach the keys easily and I'll do what I want to with those values.

json_data["tags"]["tags"]

Takes you to the list of dictionaries that you wanna iterate it with a for loop.

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