那是地图,而不是“JsonSlurper 对象”
假设你有类似的东西:
def object = [[id:5017,feature:'age',value:20],[id:2017,feature:'city',value:'paris'],[id:3017,feature:'country',value:'france']]
然后做:
def json = new JsonBuilder(object).toPrettyString()
然后json
将是一个漂亮的 json 表示,如:
[
{
"id": 5017,
"feature": "age",
"value": 20
},
{
"id": 2017,
"feature": "city",
"value": "paris"
},
{
"id": 3017,
"feature": "country",
"value": "france"
}
]
要进行转换,只需执行以下操作:
def transformed = object.collectEntries { [it.feature, it.value] }
def json = new JsonBuilder(transformed).toPrettyString()