作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从标准查询中得到了这样的结果:
[
[id:79, accountName:a99name, layoutPathId:40, layoutPath:/etc/test.txt, dataTypeId:5, dataType:demo1, vendorId:34, vendorName:v21],
[id:79, accountName:a99name, layoutPathId:41, layoutPath:/etc/test1.txt, dataTypeId:5, dataType:demo1, vendorId:34, vendorName:v21],
[id:79, accountName:a99name, layoutPathId:42, layoutPath:/etc/test3.txt, dataTypeId:6, dataType:demo2, vendorId:35, vendorName:v22]
]
{
"account": {
"id": 76,
"name": "a99name",
"datatypes": [
{
"id": 5,
"name": "demo1",
"vendors": [
{
"id": 34,
"name": "v22",
"layouts": [
{
"layoutPath": "/etc/test3.txt",
"id": "42"
},
....
]
},
......
]
},
.......
]
}
}
最佳答案
因此,鉴于:
def input = [
[id:79, accountName:'a99name', layoutPathId:40, layoutPath:'/etc/test.txt', dataTypeId:5, dataType:'demo1', vendorId:34, vendorName:'v21'],
[id:79, accountName:'a99name', layoutPathId:41, layoutPath:'/etc/test1.txt', dataTypeId:5, dataType:'demo1', vendorId:34, vendorName:'v21'],
[id:79, accountName:'a99name', layoutPathId:42, layoutPath:'/etc/test3.txt', dataTypeId:6, dataType:'demo2', vendorId:35, vendorName:'v22']
]
def munged = [ account: [
id: input.id.head(),
name: input.accountName.head(),
dataTypes: input.groupBy({it.dataTypeId}).collect { did, dataType ->
[ id: did,
name: dataType.dataType.head(),
vendors: dataType.groupBy {it.vendorId}.collect { vid, vendor ->
[ id: vid,
name: vendor.vendorName.head(),
layouts: vendor.groupBy { it.layoutPathId }.collect { lid, layout ->
[ id: lid,
layoutPath: layout.layoutPath.head()
]
}
]
}
]
}
]]
JsonBuilder
传递此新 map :
println new groovy.json.JsonBuilder(munged).toPrettyString()
{
"account": {
"id": 79,
"name": "a99name",
"dataTypes": [
{
"id": 5,
"name": "demo1",
"vendors": [
{
"id": 34,
"name": "v21",
"layouts": [
{
"id": 40,
"layoutPath": "/etc/test.txt"
},
{
"id": 41,
"layoutPath": "/etc/test1.txt"
}
]
}
]
},
{
"id": 6,
"name": "demo2",
"vendors": [
{
"id": 35,
"name": "v22",
"layouts": [
{
"id": 42,
"layoutPath": "/etc/test3.txt"
}
]
}
]
}
]
}
}
关于json - 如何将 map 列表转换为常规的groovy/grails对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32948054/
我是一名优秀的程序员,十分优秀!