Grails中带有嵌套数组的Groovy JSONBuilder

Grails中带有嵌套数组的Groovy JSONBuilder,grails,groovy,jsonbuilder,Grails,Groovy,Jsonbuilder,有没有办法在Groovy中使用JSONBuilder嵌套JSON数组?更明确地说,我有一个Grails应用程序,它需要呈现如下内容: { "event": { "type": "1.0", "templates": [ { "template":{ "window": { "type1": "id-1",

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

{
    "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"
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}
我在让Grails控制器使用
render
函数以及在服务中显式使用JSONBuilder来构建它时遇到了一些问题

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

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")
                        }
                    }
                }
            }
        }
    }
}

数组
闭包中缺少一个级别。试试这个:

templates = array {
  item {
    template = {
      window = {
        // ...

数组
闭包中缺少一个级别。试试这个:

templates = array {
  item {
    template = {
      window = {
        // ...

这似乎对我不起作用。我收到以下错误消息:
Misplaced key:应为key模式,但为OBJECT。Stacktrace如下:Message:Misplaced key:key的预期模式,但在grails.converters.JSON中是对象行| Method->>199 |值
对不起,我的错误。我最初的建议不起作用。经过更多的实验后,我想出了一些可行的方法,并根据这些方法编辑了我的答案。我收到以下错误消息:
Misplaced key:应为key模式,但为OBJECT。Stacktrace如下:Message:Misplaced key:key的预期模式,但在grails.converters.JSON中是对象行| Method->>199 |值
对不起,我的错误。我最初的建议不起作用。经过更多的实验,我想出了一些有效的方法,并相应地编辑了我的答案