简体   繁体   English

Grails的。 JSON。 他的方法?

[英]Grails. JSON. his methods?

I was up all night looking for information on how to use JSON in Grails . 我整夜都在寻找有关如何在Grails使用JSON信息。 And I found the information that in JSON closure , we can use only one method - array - no other. 我发现在JSON closure ,我们只能使用一种方法- array -不能使用其他方法。 is it true? 是真的吗

and next small question: 下一个小问题:

i find here: http://www.jiramot.info/mini-guide-to-rendering-json-with-grails example: 我在这里找到: http : //www.jiramot.info/mini-guide-to-rendering-json-with-grails示例:

render(contentType:‘text/json’){
        collection{
                pair(name:‘value’)
                pair(name:‘value1′)
        }
}

Will be rendered as: 将呈现为:

{collection:[{"name":"value"},{"name":"value1"}]

but when i try do it, i have this result: 但是当我尝试这样做时,我得到以下结果:

{"collection":{"pair":{"name":"value1"}}}

What conclusion can we do? 我们能得出什么结论? i do some wrong or wrong example on site? 我在网站上做了一些错误或错误的例子吗?

try 尝试

render(contentType:‘text/json’){
        collection {
                array {
                   pair(name:"value")
                   pair(name:"value1")
                }
        }
}

JSON rendering in Grails is much easier... Grails中的JSON渲染要容易得多...

def foobar = [ fooArray: [ 1, 2, 3 ], bar: [ b: "bbb", a: "aaa", r: "rrr" ] ]
render foobar as JSON

results in 结果是

{"fooArray":[1,2,3],"bar":{"b":"bbb","a":"aaa","r":"rrr"}}

Just create an appropriate Groovy structure and render it as JSON . 只需创建一个合适的Groovy结构并将其render as JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM