在Jenkins中解析JSON对象
我在解析这个json文件时遇到一些问题。似乎我在解析对象keyname时遇到了一些问题,我不太确定如何解决这个问题 我的在Jenkins中解析JSON对象,jenkins,groovy,Jenkins,Groovy,我在解析这个json文件时遇到一些问题。似乎我在解析对象keyname时遇到了一些问题,我不太确定如何解决这个问题 我的JenkinsfileProperties.json文件如下所示: { "predefines": { "69dA06x01": { "customer": "hello1", "label": "test1", "opt": true, "li
JenkinsfileProperties.json
文件如下所示:
{
"predefines": {
"69dA06x01": {
"customer": "hello1",
"label": "test1",
"opt": true,
"license": "baseline"
},
"69dR08x06": {
"customer": "hello2",
"label": "test2",
"opt": true,
"license": "baseline"
}
}
}
conf = getJobConfiguration scm: scm, local: "checkout", file: "JenkinsfileProperties.json"
conf.predefines.each { predef ->
builds["Build ${predef}"] = build(predef)
lints["Lint ${predef}"] = lint(predef)
unitTests["Unit Test ${predef}"] = unitTest(predef, predef.customer)
}
predef.value["customer"]
predef.value["opt"]
predef.value["license"]
predef.value["label"]
我的groovy文件如下所示:
{
"predefines": {
"69dA06x01": {
"customer": "hello1",
"label": "test1",
"opt": true,
"license": "baseline"
},
"69dR08x06": {
"customer": "hello2",
"label": "test2",
"opt": true,
"license": "baseline"
}
}
}
conf = getJobConfiguration scm: scm, local: "checkout", file: "JenkinsfileProperties.json"
conf.predefines.each { predef ->
builds["Build ${predef}"] = build(predef)
lints["Lint ${predef}"] = lint(predef)
unitTests["Unit Test ${predef}"] = unitTest(predef, predef.customer)
}
predef.value["customer"]
predef.value["opt"]
predef.value["license"]
predef.value["label"]
在我的头脑中,config.predefines.each{predef->
将为我提供69d….
的每个实例及其子项。因此访问子项只需执行predef.customer
或predef.label
等操作
现在我发现没有找到这样的字段:字段java.util.AbstractMap$SimpleImmutableEntry customer。
我做错了什么
我需要能够遍历69…
条目,还必须能够获取它们的值,即69dA06x01
就这样解决了
键值(69d…
)的访问方式如下:
predef.key
键子项的访问方式如下:
{
"predefines": {
"69dA06x01": {
"customer": "hello1",
"label": "test1",
"opt": true,
"license": "baseline"
},
"69dR08x06": {
"customer": "hello2",
"label": "test2",
"opt": true,
"license": "baseline"
}
}
}
conf = getJobConfiguration scm: scm, local: "checkout", file: "JenkinsfileProperties.json"
conf.predefines.each { predef ->
builds["Build ${predef}"] = build(predef)
lints["Lint ${predef}"] = lint(predef)
unitTests["Unit Test ${predef}"] = unitTest(predef, predef.customer)
}
predef.value["customer"]
predef.value["opt"]
predef.value["license"]
predef.value["label"]
总而言之:
conf.predefines.each { predef ->
builds["Build ${predef.key}"] = build(predef)
lints["Lint ${predef.key}"] = lint(predef)
unitTests["Unit Test ${predef.key}"] = unitTest(predef, predef.value["customer"])
}
如果子项始终相同,则此方法有效,但我可能会看到这样一种情况,即在每个69d…
对象中,所有子项都被称为不同的东西。幸运的是,此方法适用于此情况