I have the following json,
"Location": "abc",
"Codes":
[
{
"high": "xyz",
"low": "aaa"
}
]
I am doing Data driven testing using SOAP UI. In the above code I am displaying the "Location" attribute json value in groovy script by using below code
def jsonRes = slurper.parseText(responseJson)
def String LocationJson = jsonRes.Location
log.info ("location is " +LocationJson)
Can anyone suggest me how I display the json values of "high" and "low" which inside "Codes" List?
The "[" and "]" are used to create arrays. So you will need to use a [0] to access the first element of that array. Or use a loop structure, that lets you work directly on each element of the array, like I've done in the example below.
I modified your code. I hope you are able to run it without modifications.
def jsonstring = '{"Location": "abc","Codes": [ { "high": "xyz", "low": "aaa" } ] }"'
log.info jsonstring
def slurper = new groovy.json.JsonSlurper()
def jsonRes = slurper.parseText(jsonstring)
def LocationJson = jsonRes.Location
log.info ("location is " +LocationJson)
// this will loop through all Codes element...
for (def codeElement : jsonRes.Codes) {
log.info ("high is " + codeElement.high)
log.info ("low is " + codeElement.low)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.