(Quick Reference)

6.1.8 关于JSONBuilder - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

6.1.8 关于JSONBuilder

The previous section on on XML and JSON responses covered simplistic examples of rendering XML and JSON responses. Whilst the XML builder used by Grails is the standard XmlSlurper found in Groovy, the JSON builder is a custom implementation specific to Grails.

JSONBuilder and Grails versions

JSONBuilder behaves different depending on the version of Grails you use. For version below 1.2 the deprecated grails.web.JSONBuilder class is used. This section covers the usage of the Grails 1.2 JSONBuilder

For backwards compatibility the old JSONBuilder class is used with the render method for older applications; to use the newer/better JSONBuilder class set the following in Config.groovy:

grails.json.legacy.builder = false

在以前关于XML和JSON响应的章节中,我们曾经简单地涉猎到渲染XML和JSON响应的例子。而Grails的XML生成器是Groovy标准的 XmlSlurper ,JSON生成器是Grails自己实现的一个规范。

JSONBuilder和Grails版本

JSONBuilder的行为根据你使用的Grails版本而有所不同。对于版本低于1.2,使用的是可以被废弃的grails.web.JSONBuilder类。本节将涉及到Grails 1.2 JSONBuilder的用法。

因为要兼容以前版本的原因,旧的JSONBuilder类被用于旧应用的render方法;而要使用更新更好的JSONBuilder类,需要在Config.groovy中做如下设置:

grails.json.legacy.builder = false

Rendering Simple Objects

To render a simple JSON object just set properties within the context of the Closure:

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

The above will produce the JSON:

{"hello":"world"}

渲染简单对象

要渲染简单的JSON对象,只需要在闭包的上下内设置属性即可:

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

上述将产生如下JSON输出:

{"hello":"world"}

Rendering JSON Arrays

To render a list of objects simple assign a list:

render(contentType: "text/json") {
    categories = ['a', 'b', 'c']
}

This will produce:

{"categories":["a","b","c"]}

You can also render lists of complex objects, for example:

render(contentType: "text/json") {
    categories = [ { a = "A" }, { b = "B" } ]
}

This will produce:

{"categories":[ {"a":"A"} , {"b":"B"}] }

Use the special element method to return a list as the root:

render(contentType: "text/json") {
    element 1
    element 2
    element 3
}

The above code produces:

[1,2,3]

渲染JSON数组

要渲染一个对象的列表,只需要简单给其赋值一个列表即可:

render(contentType: "text/json") {
    categories = ['a', 'b', 'c']
}

这将输出:

{"categories":["a","b","c"]}

你也可以渲染复杂对象的列表,比如:

render(contentType: "text/json") {
    categories = [ { a = "A" }, { b = "B" } ]
}

这将输出:

{"categories":[ {"a":"A"} , {"b":"B"}] }

使用特定的element方法可以返回一个根范围的列表:

render(contentType: "text/json") {
    element 1
    element 2
    element 3
}

上述代码产生如下输出:

[1,2,3]

Rendering Complex Objects

Rendering complex objects can be done with Closures. For example:

render(contentType: "text/json") {
    categories = ['a', 'b', 'c']
    title = "Hello JSON"
    information = {
        pages = 10
    }
}

The above will produce the JSON:

{"categories":["a","b","c"],"title":"Hello JSON","information":{"pages":10}}

渲染复杂对象

渲染复杂对象要在闭包内完成,比如:

render(contentType: "text/json") {
    categories = ['a', 'b', 'c']
    title = "Hello JSON"
    information = {
        pages = 10
    }
}

上述将输出JSON:

{"categories":["a","b","c"],"title":"Hello JSON","information":{"pages":10}}

Arrays of Complex Objects

As mentioned previously you can nest complex objects within arrays using Closures:

render(contentType: "text/json") {
    categories = [ { a = "A" }, { b = "B" } ]
}

You can use the array method to build them up dynamically:

def results = Book.list()
render(contentType: "text/json") {
    books = array {
        for (b in results) {
            book title: b.title
        }
    }
}

复杂对象数组

如前面所提到的,你可以在闭包内使用嵌套的复杂对象来实现数组:

render(contentType: "text/json") {
    categories = [ { a = "A" }, { b = "B" } ]
}

你也可以使用array方法来动态地构建它们:

def results = Book.list()
render(contentType: "text/json") {
    books = array {
        for (b in results) {
            book title: b.title
        }
    }
}

Direct JSONBuilder API Access

If you don't have access to the render method, but still want to produce JSON you can use the API directly:

def builder = new JSONBuilder()

def result = builder.build { categories = ['a', 'b', 'c'] title = "Hello JSON" information = { pages = 10 } }

// prints the JSON text println result.toString()

def sw = new StringWriter() result.render sw

直接使用JSONBuilder API

如果你不能使用render方法,你还是可以通过直接使用API来产生JSON的:

def builder = new JSONBuilder()

def result = builder.build { categories = ['a', 'b', 'c'] title = "Hello JSON" information = { pages = 10 } }

// prints the JSON text println result.toString()

def sw = new StringWriter() result.render sw