Wednesday, September 11, 2013

How to parse JSON in Java

JSON slurper which parses text or reader content into a data structure of lists and maps.

Example usage:
def slurper = new JsonSlurper()
def result = slurper.parseText('{"person":{"name":"Guillaume","age":33,"pets":["dog","cat"]}}')

println "Name: "+result.person.name;
println "Age: "+result.person.age;
println "Pet Size: "+result.person.pets.size();
println "Pet[0]: "+result.person.pets[0];
println "Pet[1]: "+result.person.pets[1];

Output:
Name: Guillaume
Age: 33
Pet Size: 2
Pet[0]: dog
Pet[1]: cat
 
http://groovy.codehaus.org/gapi/groovy/json/JsonSlurper.html

No comments:

Post a Comment