Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.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
Java Grails将自定义查询检索到映射列表_Java_Grails - Fatal编程技术网

Java Grails将自定义查询检索到映射列表

Java Grails将自定义查询检索到映射列表,java,grails,Java,Grails,我是grails的新手 我想为jquery自动完成创建一个控制器 def arrSong = Song.executeQuery("select id, title as value, concat(artist, ' - ', title) as label from ${Song.name} where title like concat('%', :paramTitle, '%')", [paramTitle:params.term?.toString()]) render arrSong

我是grails的新手

我想为jquery自动完成创建一个控制器

def arrSong = Song.executeQuery("select id, title as value, concat(artist, ' - ', title) as label from ${Song.name} where title like concat('%', :paramTitle, '%')", [paramTitle:params.term?.toString()])
render arrSong as JSON
通过这段代码,我得到了以下JSON:

[[1,"Mr. Brightside","The Killers - Mr. Brightside"]]
我的期望是:

[{"id":1,"value":"Mr. Brightside","label":"The Killers - Mr. Brightside"}]

任何人都可以提供帮助?

executeQuery
您编写的方式将返回一个未命名结果列表,必须使用索引检索该列表

例如:

[[1,"Mr. Brightside","The Killers - Mr. Brightside"]]

arrSong.each{println it[0]} 
//Prints 1, similarly it[1] for "Mr. Brightside" and so on
您需要的是如下所示的结果集的
映射
表示,然后您应该能够轻松地将映射呈现为JSON

def query = """
             select new map(id as id, 
                            title as value, 
                            concat(artist, ' - ', title) as label) 
             from ${Song.name} 
             where title like concat('%', :paramTitle, '%')
            """

def arrSong = Song.executeQuery(query, [paramTitle: params.term?.toString()])

render arrSong as JSON
应该回来

[{"id":1,"value":"Mr. Brightside","label":"The Killers - Mr. Brightside"}]

查看AjaxDependencySelection插件。它提供gsp的自动完成功能