0

我有以下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
}

我想将 的feature值转换JsonSluperJsonObject

4

1 回答 1

0

那是地图,而不是“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()
于 2016-04-22T10:23:13.153 回答