6.1.7 XML and JSON Responses

h4. Using the render method to output XML
h4. 使用render输出XML
Grails' supports a few different ways to produce XML and JSON responses. The first one covered is via the [render|controllers] method. 
Grails提供一些不同的方法来产生XML和JSON响应。第一个是隐式的通过[render|controllers]方法。
The @render@ method can be passed a block of code to do mark-up building in XML:
@render@方法可以传递一个代码块来执行标记生成器产生XML
{code}
def list = {
	def results = Book.list()
	render(contentType:"text/xml") {
		books {
			for(b in results) {
				book(title:b.title)
			}
		}	
	}
}
{code}                            

The result of this code would be something like:
这段代码的结果将会像这样:
{code:xml}
<books>
	  <book title="The Stand" />
	  <book title="The Shining" />	
</books>
{code}

Note that you need to be careful to avoid naming conflicts when using mark-up building. For example this code would produce an error:
 注意,当你使用标记生成器时,必须小心避免命名冲突。例如,这段代码将产生一个错误:
{code}
def list = {
	def books = Book.list()  // naming conflict here
	render(contentType:"text/xml") {
		books {
			for(b in results) {
				book(title:b.title)
			}
		}	
	}
}
{code} 

The reason is that there is local variable @books@ which Groovy attempts to invoke as a method.
原因是,这里的一个本地变量@books@企图作为方法被调用。

h4. Using the render method to output JSON
h4. 使用 render 方法 输出 JSON
The @render@ method can also be used to output JSON:
@render@ 方法可以同样被用于输出JSON:
{code}
def list = {
	def results = Book.list()
	render(contentType:"text/json") {
		books {
			for(b in results) {
				book(title:b.title)
			}
		}	
	}
}
{code}     

In this case the result would be something along the lines of:
在这种情况下,结果就会是大致相同的:
{code}
[
	{title:"The Stand"}, 
	{title:"The Shining"}
]
{code}

Again the same dangers with naming conflicts apply to JSON building.
同样的命名冲突危险适用于JSON生成器。
h4. Automatic XML Marshalling
h4. 自动 XML 列集(Marshalling)(在此附上对于列集(Marshalling)解释:对函数参数进行打包处理得过程,因为指针等数据,必须通过一定得转换,才能被另一组件所理解。可以说列集(Marshalling)是一种数据格式的转换方法。)
Grails also supports automatic marshaling of [domain classes|guide:gorm] to XML via special converters.
Grails同样支持自动列集(Marshalling)[domain classes|guide:gorm]为XML通过特定的转换器。
To start off with import the @grails.converters@ package into your controller:                                     
首先,导入@grails.converters@ 类包到你的控制器(Controllers)中:            
{code}
import grails.converters.*
{code}

Now you can use the following highly readable syntax to automatically convert domain classes to XML:
现在,你可以使用下列高度易读的语法来自动转换领域类成XML:
{code}
render Book.list() as XML
{code}

The resulting output would look something like the following::
输出结果看上去会像下列这样:
{code}
<?xml version="1.0" encoding="ISO-8859-1"?>
<list>
  <book id="1">
    <author>Stephen King</author>
    <title>The Stand</title>
  </book>
  <book id="2">
    <author>Stephen King</author>
    <title>The Shining</title>
  </book>
</list>                          
{code}     

An alternative to using the converters is to use the [codecs|guide:codecs] feature of Grails. The codecs feature provides [encodeAsXML|guide:codecs] and [encodeAsJSON|guide:codecs] methods:
一个使用转换器的替代方法是使用Grails的[codecs|guide:codecs]特性。codecs特性提供了[encodeAsXML|guide:codecs]和[encodeAsJSON|guide:codecs]方法:
{code}
def xml = Book.list().encodeAsXML()
render xml
{code}

For more information on XML marshaling see the section on [REST|guide:REST]
更多的XML 列集(Marshalling)信息见[REST|guide:REST]部分
h4. Automatic JSON Marshalling
h4. 自动JSON列集(Marshalling)
Grails also supports automatic marshaling to JSON via the same mechanism. Simply substitute @XML@ with @JSON@:
Grails同样支持自动列集(Marshalling)为JSON通过同样的机制。简单替代@XML@ 为@JSON@
{code}
render Book.list() as JSON
{code}

The resulting output would look something like the following:
输出结果看上去会像下列这样:
{code}
[
	{"id":1,
	 "class":"Book",
	 "author":"Stephen King",
	 "title":"The Stand"},
	{"id":2,
	 "class":"Book",
	 "author":"Stephen King",
	 "releaseDate":new Date(1194127343161),
	 "title":"The Shining"}
 ]
{code}

Again as an alternative you can use the @encodeAsJSON@ to achieve the same effect.
再次作为一种替代,你可以使用@encodeAsJSON@达到相同的效果
 附上满江红的Grails翻译,因为雪灾的缘故,进度可能被我拖后了,也是为了让大家参数进来,如果翻译有什么不正确的地方可以及时提出来,进行更正。

你可能感兴趣的:(json,xml,REST,grails,groovy)