I'm pretty used to Grails converters, where you can convert any object to a JSON representation just like this (http://grails.org/Converters+Reference)
return foo as JSON   But in plain groovy, I cannot find an easy way to do this (http://groovy-lang.org/json.html)
JSONObject.fromObject(this)   return empty json strings...
Am I missing an obvious Groovy converter ? Or should I go for jackson or gson library ?
JsonSlurper is a class that parses JSON text or reader content into Groovy data structures (objects) such as maps, lists and primitive types like Integer , Double , Boolean and String . The class comes with a bunch of overloaded parse methods plus some special methods such as parseText , parseFile and others.
Get an Array Element Your JSON response may also contain arrays. Like any array in Java or Groovy, you can access an array element using arrayName[index] . In this example, we access the item with the index of 0, the first item in the array (the index is zero-based).
Do you mean like:
import groovy.json.*  class Me {     String name }  def o = new Me( name: 'tim' )  println new JsonBuilder( o ).toPrettyString() 
                        I couldn't get the other answers to work within the evaluate console in Intellij so...
groovy.json.JsonOutput.toJson(myObject)   This works quite well, but unfortunately
groovy.json.JsonOutput.prettyString(myObject)   didn't work for me.
To get it pretty printed I had to do this...
groovy.json.JsonOutput.prettyPrint(groovy.json.JsonOutput.toJson(myObject)) 
                        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