如何解析 JsonSluper 对象?

How to Parse a JsonSluper object?

我有以下 JsonSluper 对象:

[ [id:5017,feature:age,value:20],
  [id:2017,feature:city,value:paris],
  [id:3017,feature:country,value:france] ]

我想得到以下 JsonObject:

"person":{
    "age":20,
    "city":paris,
    "country":france
}

我想将 JsonSluperfeature 值转换为 JsonObject

的字段

这是地图,不是 "JsonSlurper object"

假设你有类似的东西:

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()