我是grails的新手......
我想为我的jquery自动完成
创建一个Controllerdef 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"}]
任何人都可以提供帮助吗?
答案 0 :(得分: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"}]