jenkins-pipeline readJSON - 如何访问嵌套元素

标签 jenkins groovy jenkins-pipeline jenkins-plugins jenkins-groovy

我在使用 readJSON 访问嵌套 JSON 时遇到问题

oldJson 字符串:

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

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

assert oldJson["rc"] == '1'

但是失败了。我认为这是因为“rc”嵌套在“type-02”中。我怎样才能访问它?

最佳答案

您始终可以使用括号表示法或点表示法通过嵌套元素的嵌套键获取嵌套元素的值。

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

        }
    }
}

输出:

[Pipeline] stage
[Pipeline] { (Read-JSON)
[Pipeline] script
[Pipeline] {
[Pipeline] readJSON
[Pipeline] echo
1
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage

关于jenkins-pipeline readJSON - 如何访问嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58397990/

相关文章:

android - 自动化gradle从代码构建应用程序包(.apk)

jenkins - 如何从 jenkins gerrit trigger 对自定义标签进行投票?

eclipse - 如何使用 Jenkins 创建 Kony 项目的构建 - 持续集成

groovy - 如何使用 @ClosureParams 来实现需要两个参数的闭包?

jenkins-pipeline - 如何在 groovy 中使用配置文件提供程序插件

hudson - 如何配置 Jenkins StarTeam 插件?

validation - 如何在Groovy中针对特定格式对字段进行验证

hibernate - grails/groovy 域类中 TimeDuration 的 Hibernate 类型是否正确?

windows - Jenkins Pipeline 无法在 Windows slave 中执行 SH 命令文件

Jenkins Blue Ocean 安装警告