在Jenkins中解析JSON对象

问题描述 投票:1回答:1

我在解析此json文件时遇到一些问题。似乎我在解析object-keyname时遇到一些问题,我不确定如何解决这个问题。

我的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)
}

在我的脑海中,config.predefines.each { predef ->将给我69d....的每个实例及其子代。因此,访问孩子只是进行predef.customerpredef.label等的事情。

现在我得到No such field found: field java.util.AbstractMap$SimpleImmutableEntry customer.我做错了什么?

我需要能够遍历69...条目,而且我还必须能够获取它们的值,即69dA06x01

jenkins groovy
1个回答
1
投票

已解决此问题

键值(69d...)的访问方式如下:predef.key

键子条目的访问方式如下:

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...对象中的所有子条目都被称为不同的事物。幸运的是,此方法适用于此方案。

© www.soinside.com 2019 - 2024. All rights reserved.