How to string json as follows:
{
"1":[{"id":6397891,"rate":81,"type":2,"unitId":1,"userId": 7133}, {"id":6397882,"rate":72,"type":1,"unitId":1,"userId":7133}],
"2":[{"id":6397906 ,"rate":90,"type":1,"unitId":2,"userId":7133}]
}
Convert to: Map<String, List<Unit>> Type
You can use jackson, fastjson, or jsoblib.
Please give me some advice!
Haha, it seems it’s too late... The questioner has already accepted the answer, but the
lamdba
method still needs to be strongly favored by the questioner, because the code is much simpler (usingfastjson
ha, but Others should be similar)The idea is that the
json
string of the question subject is actually in the form of akey-value
, which should be the structure that satisfies the final question subject's desiredMap<String, List<Unit>>
, so just useCollectors.toMap
and you’re doneThen...that's it...just this little code...(
s
is yourjson
string)