首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grails中带有嵌套数组的Groovy JSONBuilder

Grails中带有嵌套数组的Groovy JSONBuilder
EN

Stack Overflow用户
提问于 2012-06-30 03:43:20
回答 1查看 2.7K关注 0票数 1

有没有办法在Groovy中使用JSONBuilder来嵌套JSON数组?更明确地说,我有一个Grails应用程序,它需要呈现如下内容:

代码语言:javascript
复制
{
    "event": {
        "type": "1.0",
        "templates": [
            {
                "template":{
                    "window": {
                        "type1": "id-1",
                        "type2": "id-2"
                    },
                    "object": {
                        "id-1": {
                            "type": "classA",
                            "others": [
                                {
                                    "var": "thing1",
                                    "mixed": "no"
                                }
                            ]
                        },
                        "id-2": {
                            "type": "classB",
                            "others": [
                                {
                                    "var": "thing1",
                                    "mixed": "yes"
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

我在使用render函数以及在服务中显式使用JSONBuilder来构建Grails控制器时遇到了一些问题。

除了"templates“数组中的"template”对象没有被呈现之外,一切似乎都正常。下面是执行渲染的代码:

代码语言:javascript
复制
render(contentType: "text/json") {
    event {
        type = "1.0"
        templates = array {
            template = {
                window = {
                    type1 = "id-1"
                    type2 = "id-2"
                }
                object = {
                    "${ 'id-1' }" {
                        type = "classA"
                        others = array {
                            otherArr(var:"thing1", mixed:"yes")
                        }
                    }
                    "${ 'id-2' }" {
                        type = "classB"
                        others = array {
                            otherArr(var:"thing1", mixed:"yes")
                        }
                    }
                }
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-30 06:24:53

您在array闭包中缺少一个级别。试试这个:

代码语言:javascript
复制
templates = array {
  item {
    template = {
      window = {
        // ...
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11268012

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档