Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Groovy中呈现JSON_Json_Grails_Groovy - Fatal编程技术网

在Groovy中呈现JSON

在Groovy中呈现JSON,json,grails,groovy,Json,Grails,Groovy,我有以下代码在Groovy中实现JSON: def index = { def list = WordList.list() render(contentType:"text/json"){ LISTS { for(item in list){ LIST (NAME: item.name, ID: item.id); } } } } 这几乎可以正常工作,但它不

我有以下代码在Groovy中实现JSON:

def index = {
    def list = WordList.list()
    render(contentType:"text/json"){
        LISTS {
            for(item in list){
                LIST (NAME: item.name, ID: item.id);
            }
        }
    }
}

这几乎可以正常工作,但它不会显示多个结果,即名称和ID字段在每个循环中被覆盖,从而只返回最后一条记录。让它工作的正确语法是什么?

在本例中,我的解决方案是显式构造JSON映射,然后将其呈现为JSON

例如:

def list = WordList.list()
def json = []
list.each{ item ->
    json << [name: item.name, id: item.id]
}
render json as JSON
def list=WordList.list()
def json=[]
列表。每个{项->

json在本例中,我的解决方案是显式构造json映射,然后将其呈现为json

例如:

def list = WordList.list()
def json = []
list.each{ item ->
    json << [name: item.name, id: item.id]
}
render json as JSON
def list=WordList.list()
def json=[]
列表。每个{项->

json谢谢!我如何在每个单独的名称/id之前添加json标记“LIST”?tim_yates说,你不能有一个包含多个相同键Yea的映射。一旦你意识到json只是一个映射,它会让你更轻松。谢谢!我如何在每个单独的名称/id之前添加json标记“LIST”?你不能有一个包含多个相同键Yea的映射,whatim_yates说,一旦你意识到JSON只是一张地图,它会让事情变得更容易。