【问题标题】:jenkins-pipeline readJSON - how to access nested elementjenkins-pipeline readJSON - 如何访问嵌套元素
【发布时间】:2019-10-15 15:19:16
【问题描述】:

我在使用 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”中。我怎样才能访问它?

【问题讨论】:

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


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2013-01-12
      • 2019-02-08
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      相关资源
      最近更新 更多