how to parse json using groovy
Have you tried using JsonSlurper?
Example usage:
def slurper = new JsonSlurper()def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')assert result.person.name == "Guillaume"assert result.person.age == 33assert result.person.pets.size() == 2assert result.person.pets[0] == "dog"assert result.person.pets[1] == "cat"
That response is a Map, with a single element with key '212315952136472'. There's no 'data' key in the Map. If you want to loop through all entries, use something like this:
JSONObject userJson = JSON.parse(jsonResponse)userJson.each { id, data -> println data.link }
If you know it's a single-element Map then you can directly access the link
:
def data = userJson.values().iterator().next()String link = data.link
And if you knew the id (e.g. if you used it to make the request) then you can access the value more concisely:
String id = '212315952136472'...String link = userJson[id].link