我在使用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”中。我如何访问它?
发布于 2019-10-15 23:47:21
您总是可以使用括号表示法或点表示法通过嵌套键获取嵌套元素的值。
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] // stagehttps://stackoverflow.com/questions/58397990
复制相似问题