user2514963
user2514963

Reputation: 156

convert plain old groovy to JSON

what's the simplest way to respond in json using grails? E.G. the following doesn't work.

boolean a = false
render a as JSON

Upvotes: 2

Views: 1970

Answers (4)

Roberto Guerra
Roberto Guerra

Reputation: 360

render(contentType: "application/json"){
    message{
        a.each{val->
            value(val)
        }
    }
 }

Upvotes: 0

Kaleb Brasee
Kaleb Brasee

Reputation: 51915

Grails requires that the target of the JSON converter be something that can be represented as a collection of name/value pairs or an ordered list. So an object such as a map or list would work. And non-primitive objects should also work, since they can be represented as a map of properties.

In your case, something like this would work:

def a = []
a << false
render a as JSON

Upvotes: 2

hvgotcodes
hvgotcodes

Reputation: 120168

directly from the documentation, something like

render(contentType: "text/json") {
    hello = "world"
}

Upvotes: 0

Dylan Bijnagte
Dylan Bijnagte

Reputation: 1356

Use grails.converter.JSON and you can build the json map directly

http://manbuildswebsite.com/2010/02/08/rendering-json-in-grails-part-2-plain-old-groovy-objects-and-domain-objects/

Upvotes: -1

Related Questions