Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Jenkins中解析JSON对象_Jenkins_Groovy - Fatal编程技术网

在Jenkins中解析JSON对象

在Jenkins中解析JSON对象,jenkins,groovy,Jenkins,Groovy,我在解析这个json文件时遇到一些问题。似乎我在解析对象keyname时遇到了一些问题,我不太确定如何解决这个问题 我的JenkinsfileProperties.json文件如下所示: { "predefines": { "69dA06x01": { "customer": "hello1", "label": "test1", "opt": true, "li

我在解析这个json文件时遇到一些问题。似乎我在解析对象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)
}
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…
对象中,所有子项都被称为不同的东西。幸运的是,此方法适用于此情况