Jenkins 管道:如何在 JSON 对象中迭代以获取我想要的信息?
Jenkins Pipeline: How to iterate in JSON object to get information I want?
我正在尝试使用 AQL 从存储在 Artifactory 中的 docker 图像中获取一些属性。在我的 Jenkins 管道中,我能够点击 aql api,它 returns a JSON 但我无法遍历响应以获得我需要的东西,这就是价值共 key.IWant
.
这是我在 Jenkinsfile 中使用的 Groovy 代码的一部分。如您所见,我尝试以两种不同的方式进行迭代,其中 none 有效。
// Get the JSON object from the response
def json = readJSON text: response.content
println "The follow json obj is ${json}"
def latestProps = json['results']['properties']
echo "latestProps ${latestProps}"
latestPropsRefersTo = ''
for (def prop in latestProps) {
echo "Walked through key $prop.key and value $prop.value"
if(prop.key == 'key.IWant') {
latestPropsRefersTo = prop.value
break
}
}
echo "latestPropsRefersTo ${latestPropsRefersTo}"
def latestPropsRefersTo2 = latestProps.each { key, value ->
echo "2 - Walked through key $key and value $value"
if(key == 'key.IWant') {
return value
}
}
echo "latestPropsRefersTo2 ${latestPropsRefersTo2}"
这是我得到的输出
Success: Status code 200 is in the accepted range: 100:399
[Pipeline] readJSON
[Pipeline] echo
The follow json obj is [results:[[repo:xxx, path:xxx, name:manifest.json, modified:2021-10-18T19:10:25.062-05:00, properties:[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]], range:[start_pos:0, end_pos:1, total:1]]
[Pipeline] echo
latestProps [[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]
[Pipeline] echo
Walked through key [key.IWant, key.IDonWant] and value [01, 2021-12-17_23-59-59]
[Pipeline] echo
latestPropsRefersTo
[Pipeline] echo
2 - Walked through key [[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]] and value null
[Pipeline] echo
latestPropsRefersTo2 [[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]
[Pipeline] echo
如何将 latestPropsRefersTo(或 latestPropsRefersTo2)正确设置为值 01?
谢谢!
编辑: 我不知道为什么,但我用 prop.key.equals("key.IWant")
替换了 prop.key == 'key.IWant'
并且它起作用了 - 问题是操作员 ==
if clause
被评估为 false,我需要的属性没有被设置。
现在一切正常。谢谢大家的帮助!
您应该能够使用这些方法来遍历您的 json:
// Get the JSON object from the response
def json = readJSON text: response.content
println "The follow json obj is ${json}"
json.each { myData ->
myData.properties.each {myProperties ->
if(myProperties.key == prop.key) {
latestPropsRefersTo = myProperties.key
}
}
我正在尝试使用 AQL 从存储在 Artifactory 中的 docker 图像中获取一些属性。在我的 Jenkins 管道中,我能够点击 aql api,它 returns a JSON 但我无法遍历响应以获得我需要的东西,这就是价值共 key.IWant
.
这是我在 Jenkinsfile 中使用的 Groovy 代码的一部分。如您所见,我尝试以两种不同的方式进行迭代,其中 none 有效。
// Get the JSON object from the response
def json = readJSON text: response.content
println "The follow json obj is ${json}"
def latestProps = json['results']['properties']
echo "latestProps ${latestProps}"
latestPropsRefersTo = ''
for (def prop in latestProps) {
echo "Walked through key $prop.key and value $prop.value"
if(prop.key == 'key.IWant') {
latestPropsRefersTo = prop.value
break
}
}
echo "latestPropsRefersTo ${latestPropsRefersTo}"
def latestPropsRefersTo2 = latestProps.each { key, value ->
echo "2 - Walked through key $key and value $value"
if(key == 'key.IWant') {
return value
}
}
echo "latestPropsRefersTo2 ${latestPropsRefersTo2}"
这是我得到的输出
Success: Status code 200 is in the accepted range: 100:399
[Pipeline] readJSON
[Pipeline] echo
The follow json obj is [results:[[repo:xxx, path:xxx, name:manifest.json, modified:2021-10-18T19:10:25.062-05:00, properties:[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]], range:[start_pos:0, end_pos:1, total:1]]
[Pipeline] echo
latestProps [[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]
[Pipeline] echo
Walked through key [key.IWant, key.IDonWant] and value [01, 2021-12-17_23-59-59]
[Pipeline] echo
latestPropsRefersTo
[Pipeline] echo
2 - Walked through key [[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]] and value null
[Pipeline] echo
latestPropsRefersTo2 [[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]
[Pipeline] echo
如何将 latestPropsRefersTo(或 latestPropsRefersTo2)正确设置为值 01? 谢谢!
编辑: 我不知道为什么,但我用 prop.key.equals("key.IWant")
替换了 prop.key == 'key.IWant'
并且它起作用了 - 问题是操作员 ==
if clause
被评估为 false,我需要的属性没有被设置。
现在一切正常。谢谢大家的帮助!
您应该能够使用这些方法来遍历您的 json:
// Get the JSON object from the response
def json = readJSON text: response.content
println "The follow json obj is ${json}"
json.each { myData ->
myData.properties.each {myProperties ->
if(myProperties.key == prop.key) {
latestPropsRefersTo = myProperties.key
}
}