Using JSON builder how do I stuff a Map into each Object of Object Array
Given: myList = [request:request,actions:actions]
where reque开发者_运维知识库st is an object and actions is a map.
Trying to get something like this:
{data:[
{a:'a',b:'b',actions:[c:'c',d:'d']},..
]}
where a and b are request properties while c and d are actions are map entries. using:
render(contentType:"text/json"){
data = array {
myList.each { obj->
rqst = {
obj.request
actions = {actions: obj.actions}
}
}
}
}
obviously syntax here is wrong... but perhaps close?
I think the following accomplishes what you were looking for. PropsHolder is a simple inner class with two (a, b) properties
def request = new PropsHolder()
request.a = "a"
request.b = "b"
def actions = [c: "c", d: "d"]
def myList = [request: request, actions: actions]
render(contentType:"text/json")
{
[data: [
a: myList.request.a,
b: myList.request.b,
actions: myList.actions
]
]
}
The json result looks like this when output to a web page:
{"data":{"a":"a","b":"b","actions":{"c":"c","d":"d"}}}
I wasn't quite sure with the [a, b, actions] collection if you were looking for a map or an array. Tough to tell from the output, I went with map.
精彩评论