0

我是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"}]

任何人都可以帮忙吗?

4

1 回答 1

0

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

例如:

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

arrSong.each{println it[0]} 
//Prints 1, similarly it[1] for "Mr. Brightside" and so on

您需要的是map如下所示的结果集表示,然后您应该能够毫不费力地将地图呈现为 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"}]
于 2013-09-29T15:26:17.223 回答