gpt4 book ai didi

python - 遍历 JSON - 需要逻辑来计算值的总和

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:50:00 24 4
gpt4 key购买 nike

我正在尝试从下面的 JSON 中获取键值 number 的总和。

总和 = 193(21+52+52+68)

我将在这里使用循环而不是直接访问 - response['jobs']['0']['lastBuild']['number']。任何人都可以提供逻辑吗?任何编程语言都可以。

{
"_class": "hudson.model.Hudson",
"jobs": [
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"name": "ADP-CHINA-IT",
"lastBuild": {
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 21
}
},
{
"_class": "com.cloudbees.hudson.plugins.folder.Folder",
"name": "A",
"jobs": [
{
"_class": "hudson.model.FreeStyleProject",
"name": "dummy",
"lastBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 52
}
},
{
"_class": "com.cloudbees.hudson.plugins.folder.Folder",
"name": "F",
"jobs": [
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"name": "pipeline",
"lastBuild": {
"_class": "hudson.model.FreeStyleBuild",
"number": 52
}
}
]
}
]
},
{
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowJob",
"name": "CICD_Windows_Shared_Lib_Test",
"lastBuild": {
"_class": "org.jenkinsci.plugins.workflow.job.WorkflowRun",
"number": 68
}
}


]

最佳答案

python :

def sum_(s, dict_):
return s + reduce(sum_, dict_.get("jobs", []), 0) + dict_.get("lastBuild", {}).get("number", 0)


reduce(sum_, response["jobs"], 0)

关于python - 遍历 JSON - 需要逻辑来计算值的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55507192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com