I found many things about converting Groovy to JSON, but oddly enough, not the other way.
What is the (best) JSON to Groovy parser around there ?
Use that to create a JSON array. You then loop through the array for (int i = 0; i < array. length(); i++) and retrieve each JSON object by calling array. getJSONObject(i); which returns JSONObject .
Groovy 1.8 introduced json package for parsing and building JSON data. JsonSlurper is used for parsing JSON and JsonBuilder is used for creating JSON in Groovy. For more details visit the API here and a good article here.
If you are on Groovy 1.8 or later, there is a build in JsonSlurper you can use this way:
import groovy.json.JsonSlurper //Attention: you have to use double quotes inside the json string def jsonObj = new JsonSlurper().parseText( '{ "name":"Peter", "age": 23}' ) assert jsonObj.name == "Peter" assert jsonObj.age == 23 //this won't work, because it's not defined assert jsonObj.gender == null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With