Gson: can use gson to convert json into nested map ?

Created on 4 Nov 2015  ·  2Comments  ·  Source: google/gson

like that:
{"result": {
"update": {
"title": "双11抢10亿现金券啦",
"msg": "峰峰说,升级新版之后双十一抢购杠杠的!新版还有不一样的峰峰彩蛋哦",
},
"ts": 1446446129,
}
Map resultMap = new HashMap<>();
Map map1 = new HashMap<>();
map1.put("title","双11抢10亿现金券啦");
map1.put("msg","峰峰说,升级新版之后双十一抢购杠杠的!新版还有不一样的峰峰彩蛋哦");
resultMap.put("update",map1);
resultMap.put("ts","1446446129");

can gson do it above directly?

Most helpful comment

my type is Map>>> , map in map, the level is indefinitely ,only the deepest map is Map

All 2 comments

If your type is Map> I can't imagine why not.
You'd need to use a Type rather than a Class, obtained from TypeToken,
since you're tryign to deserialize a parameterized type.

Type typeOfT = new TypeToken<Map<String, Map<String,
String>>>(){}.getType();
Map<String, Map<String, String>> map = gson.fromJson(jsonString, typeOfT);

On Tue, 3 Nov 2015 at 17:31 lytasky [email protected] wrote:

like that:
{"result": {
"update": {
"title": "双11抢10亿现金券啦",
"msg": "峰峰说,升级新版之后双十一抢购杠杠的!新版还有不一样的峰峰彩蛋哦",
},
"ts": 1446446129,
}
Map resultMap = new HashMap<>();
Map map1 = new HashMap<>();
map1.put("title","双11抢10亿现金券啦");
map1.put("msg","峰峰说,升级新版之后双十一抢购杠杠的!新版还有不一样的峰峰彩蛋哦");
resultMap.put("update",map1);
resultMap.put("ts","1446446129");

can gson do it above directly?


Reply to this email directly or view it on GitHub
https://github.com/google/gson/issues/727.

my type is Map>>> , map in map, the level is indefinitely ,only the deepest map is Map

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GoogleCodeExporter picture GoogleCodeExporter  ·  16Comments

kramer picture kramer  ·  26Comments

GoogleCodeExporter picture GoogleCodeExporter  ·  32Comments

GoogleCodeExporter picture GoogleCodeExporter  ·  19Comments

kdehairy picture kdehairy  ·  43Comments