遍历嵌套 JSON
我有以下字典数据。
"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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如我在评论中所说,首先你想将 json 字符串解析为字典
输出:
然后你想按它的
applied_at
标签进行排序:输出:
两个输出是相同的,因为你的初始 json 文件已经排序。
然后,如果您想知道第一个应用哪个标签,只需调用
tags[0]
,第二个应用tags[1]
,依此类推print 的输出(标签[0])
As I said in comment, first you want to parse the json string into dictionary
Outputs:
Then you want to sort by it's
applied_at
tag:outputs:
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 bytags[1]
, and so onoutput of
print(tags[0])
我不明白你到底想做什么。我从您的描述中得到的是,名称为“Resolution Provided”的标签对您很重要,对于这些标签,您希望使用“applied_at”字段执行某些操作。
请注意,您提供的“JSON”实际上不是有效的 JSON。您需要在该数据周围添加一组额外的花括号,以使其成为有效的 JSON。
以下是如何提取名称为“Resolution Provided”的标签的“applied_at”字段。此代码添加了额外的花括号,以允许您的数据被解析为有效的 JSON:
结果:
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:
Result:
我能想到的第一个方法是先到达“标签”,然后我会看到一个字典列表。所以我可以在这里使用 for 循环来遍历这些字典。
然后我将轻松触及按键,并用这些值做我想做的事情。
带您到要使用 for 循环迭代的字典列表。
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.
Takes you to the list of dictionaries that you wanna iterate it with a for loop.