JSONBuilder in Grails console
I'm t开发者_Go百科rying to use the Groovy JSONBuilder in the Grails (2.0.0.M2) console. When I execute the following
import grails.web.JSONBuilder
def builder = new JSONBuilder()
def result = builder.build {
foo = 'bar'
}
result.toString()
I get the error:
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'bar' with class 'java.lang.String' to class 'grails.converters.JSON'
at grails.web.JSONBuilder.build(JSONBuilder.groovy:39)
The builder seems to work fine in the app itself. Is there a reason why it doesn't work in the console?
While i was working with JSONBuilder, i faced similar problems, then i decide to not use JSONBuilder. If it is not necessary for you, you can change your builder.
import org.codehaus.groovy.grails.web.json.*;
import grails.converters.JSON;
def result =[:]
result.foo='bar'
println result as JSON
it's result will be same.
精彩评论