这里是脚本找到相同的:
import groovy.json.*
//Using the fixed json to explain how you can retrive the data
//Of couse, you can also use dynamic value that you get
def response = '''{"names": [ { "id": 1, "name": "Ted", }, { "id": 2, "name": "Ray", }, { "id": 3, "name": "Kev", } ]}'''
//Parse the json string and get the names
def names = new JsonSlurper().parseText(response).names
//retrive the id value when name is Ray
def rayId = names.find{it.name == 'Ray'}.id
log.info "Id of Ray is : ${rayId}"
//Another way to get both position and id
names.eachWithIndex { element, index ->
if (element.name == 'Ray') {
log.info "Position : $index, And Id is : ${element.id}"
}
}
这里你可以看到输出
![enter image description here](https://i.stack.imgur.com/y6n8v.png)
来源
2016-06-23 15:53:09
Rao
如果名称是“Ray”,您是否想要查找'id'值? – Rao
别担心,我的回答添加了,请检查。 – Rao