Forum Discussion
JHunt
6 years agoCommunity Hero
You can get an ordered list with collect:
ArrayList<String> keys = [b:2, a:1].collect { key, value -> key }
assert keys == ["b", "a"]
assert keys != ["a", "b"]
- nila20206 years agoNew Contributor
Thank you for your quick response, I will try and let you know,
- nila20206 years agoNew Contributor
I am new to groovy script, Please can you explain what is [b:2,a:1], Thank you in advance
- JHunt6 years agoCommunity Hero
That's a Map. It's just like an object in JSON. So for parsing a response, and checking the keys of the object, you would do something like this:
String response = '''{ "DataValue": [ { "ABC1": "Value", "ABC2": "Value" }, { "ABC1": "Value", "ABC2": "Value" } ] }''' new groovy.json.JsonSlurper().parseText(response)["DataValue"].each { dataValue -> ArrayList<String> keys = dataValue.collect { key, value -> key } assert keys == [ "ABC1", "ABC2" ] }
And the assertion will fail if the order is different to what you are expecting.
- nila20206 years agoNew Contributor
Thank you for your response, Appreciate your help
Related Content
- 2 years ago
Recent Discussions
- 11 days ago