gpt4 book ai didi

jenkins-pipeline readJSON - 如何将关键元素读取为列表

转载 作者:行者123 更新时间:2023-12-05 01:39:21 31 4
gpt4 key购买 nike

我无法使用 readJSON 从 JSON 读取所有键“type-X.X”

oldJson 字符串:

{
"branch":{
"type-0.2":{"version":"0.2","rc":"1","rel":"1","extras":"1"}}
"type-0.3":{"version":"0.3","rc":"1","rel":"1","extras":"1"}}
}

我尝试像示例中那样访问它 https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readjson-read-json-from-files-in-the-workspace

def branchList = new JsonSlurper().parseText(oldJson['branch'])
echo (branchList.keySet().toString())

但失败了:

hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: groovy.json.JsonSlurper.parseText() is applicable for argument types: (net.sf.json.JSONObject) values:

我想得到一个列表 ["type-0.2", "type-0.3"]

最佳答案

您提供的 JSON 字符串无效。在第一个子元素之后有一个额外的 } 和一个缺失的 ,。它必须是:

{
"branch":{
"type-0.2":{"version":"0.2","rc":"1","rel":"1","extras":"1"},
"type-0.3":{"version":"0.3","rc":"1","rel":"1","extras":"1"}
}
}

现在,您可以使用管道中的 readJSON 步骤解析它,以获取键列表。

stage('Read-JSON') {
steps {
script {
def oldJson = '''{
"branch":{
"type-0.2":{"version":"0.2","rc":"1","rel":"1","extras":"1"},
"type-0.3":{"version":"0.3","rc":"1","rel":"1","extras":"1"}
}
}'''
def props = readJSON text: oldJson
def keyList = props['branch'].keySet()
echo "${keyList}"
// println(props['branch'].keySet())

}
}
}

输出:

[Pipeline] stage
[Pipeline] { (Read-JSON)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
[type-0.2, type-0.3]
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage

关于jenkins-pipeline readJSON - 如何将关键元素读取为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58412819/

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