gpt4 book ai didi

json - 在 Jenkins Pipeline 上解析 JSON (groovy)

转载 作者:行者123 更新时间:2023-12-04 23:12:17 37 4
gpt4 key购买 nike

我创建了一个方法,如在线所示:

@NonCPS
def parseJsonString(String jsonString) {
def lazyMap = new JsonSlurper().parseText(jsonString)

// JsonSlurper returns a non-serializable LazyMap, so copy it into a regular map before returning
def m = [:]
m.putAll(lazyMap)
return m
}
但我收到以下错误:

ERROR: java.io.NotSerializableException: groovy.json.internal.LazyMap


为了解决这个问题,我必须创建一个完整的方法来执行整个步骤。例如,在一个方法中,我会做与上面相同的操作,解析我想要的信息,最后将其作为字符串返回。
然而,这带来了另一个问题,特别是如果您将此方法包装在 withCredentials 中时。 ,这将需要另一个 withCredentials .

最佳答案

我终于找到了更好的解决方案!

Jenkins“Pipeline Utility Steps”插件中的 readJSON() 方法,如下所示:

https://jenkins.io/doc/pipeline/steps/pipeline-utility-steps/#readjson-read-json-from-files-in-the-workspace

这是一个示例,我们最终可以在其中抛弃丑陋的 GROOVY JSONPARSE 废话。

node() {
stage("checkout") {
def jsonString = '{"name":"katone","age":5}'
def jsonObj = readJSON text: jsonString

assert jsonObj['name'] == 'katone' // this is a comparison. It returns true
sh "echo ${jsonObj.name}" // prints out katone
sh "echo ${jsonObj.age}" // prints out 5
}
}

关于json - 在 Jenkins Pipeline 上解析 JSON (groovy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55174084/

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