Grails Domain类JSON [英] Grails Domain class JSON
本文介绍了Grails Domain类JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
response:Alert.list()
它给出:
[{id:。 。},{id:..}]
我想要的:
{alerts:[{id:...}。 {id:..}]}
如何让响应制作成我想要的格式?我不想拥有一个自定义的JSON编组器,因为我的数据集很大。
如果我进行以下操作而不是定制编组器,结果如何?
def o = new JSONObject()
def arr = new JSONArray()
def a = new JSONObject )
alerts.each {
a.put(id,it.id)
...
arr.add(a)
}
o.put(alerts,arr)
响应o
解决方案像这样尝试:
//在这里进行过滤以获取alertList
回应:[alerts:alertList]
I have this controller:
respond :Alert.list()
It gives:
[{id: ..}, {id: ..}]
What I want:
{"alerts":[{"id":...}. {id:..}]}
How do I let the respond make into the format I want? I don't want to have a custom JSON marshaller as my dataset is big.
What will be the outcome if I do the following than having a custom marshaller?
def o = new JSONObject() def arr = new JSONArray() def a = new JSONObject() alerts.each{ a.put("id",it.id) ... arr.add(a) } o.put("alerts",arr) respond o
解决方案Try it like this:
//do your filtering here to get alertList respond: [alerts: alertList]
这篇关于Grails Domain类JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文