我正在测试一个 Controller ,只是想检索JSON。但是,我注意到 Controller 返回了数据,但是它没有正确返回带有键/值对的JSON。它实际上仅返回数据。但是我注意到,如果我使用Incident.getAll(),它将正确返回JSON。例如,与仅返回数据的executreQuery相比,它将返回[INCIDENTID:“Value”]。
我的代码:
def incident = Incident.executeQuery("select a.INCIDENTID from Incident a")
render incident as JSON
最佳答案
executeQuery
的结果是您指定的属性的列表,而不是实际的域对象。例如:
TestDomain.executeQuery("select t.id from TestDomain") as JSON
===> [1, 2, 3]
如果需要域对象,则可以改用
findAll
。尝试这样的事情:TestDomain.findAll("from TestDomain") as JSON
===>[{"class":"TestDomain","id":1,"name":"one"},
{"class":"TestDomain","id":2,"name":"two"},
{"class":"TestDomain","id":3,"name":"three"}]
关于json - Grails:使用executeQuery呈现JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903388/