我在构建json消息时遇到问题,我需要json消息采用以下格式:
{
success:true,
count:3,
data: [
{id:1, data: SUCCESS},
{id:2, data: FAILURE},
{id:3, data: Not Declared}
]
}
我不确定如何做这个plz帮助
答案 0 :(得分:0)
您可以通过以下方式执行此操作:
class Data { String id; String data }
def yourdata = [
new Data("1","SUCCESS")
new Data("2","FAILURE")
new ...
]
def builder = new groovy.json.JsonBuilder()
def root = builder {
success true
count 3
data yourdata.collect { d ->
["id":d.id,
"data":d.data]
}
}
return builder.toPrettyString()