map --> json

2021-04-20 04:29

阅读:429

标签:ring   linked   type   ToJson   lin   类型   ble   tty   nbsp   

接下来介绍Map对象与json的相互转换

Map类型的转换就需要用到

GsonBuilder().enableComplexMapKeySerialization().create()类生成Gson对象
1.      Map类型的转换:

这里直接贴关键代码,其他代码可参考前两篇文章,或下载代码。

private String seven() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");

return gson2.toJson(map);
}
 

结果为:


把json数据转换为Map

private String seven() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");

String jsonString = gson.toJson(map);
Type type = new TypeToken>() {}.getType();
Map map2 = gson2.fromJson(jsonString, type);

String showString = "";
for (String keyString : map2.keySet()) {
showString += keyString + ":" + map2.get(keyString) + "\n-----\n";
}

showString += "----------------------\n";
return showString;
}
 

结果为:


2.      Map类型的转换:

private String eight() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map map = new LinkedHashMap();
map.put(new ToJsonBeanOne(1, "小熊1", 21), "value1");
map.put(new ToJsonBeanOne(2, "小熊2", 21), "value2");
map.put(new ToJsonBeanOne(3, "小熊3", 21), "value3");

return gson2.toJson(map);
}


结果为:


把json数据转换为Map

private String eight() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map map = new LinkedHashMap();
map.put(new ToJsonBeanOne(1, "小熊1", 21), "value1");
map.put(new ToJsonBeanOne(2, "小熊2", 21), "value2");
map.put(new ToJsonBeanOne(3, "小熊3", 21), "value3");

String jsonString = gson2.toJson(map);

Type type = new TypeToken>() {}.getType();
Map map2 = gson.fromJson(jsonString, type);

String showString = "";
for (ToJsonBeanOne beanOne : map2.keySet()) {
showString += "key:\n" + beanOne.toString() + "value:" + map2.get(beanOne) + "\n-----\n";
}

showString += "----------------------\n";
return showString;
}
结果为:


 

3. Map类型的转换:
private String nine() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map map = new LinkedHashMap();
map.put("key1", new ToJsonBeanOne(1, "小熊1", 21));
map.put("key2", new ToJsonBeanOne(2, "小熊2", 21));
map.put("key3", new ToJsonBeanOne(3, "小熊3", 21));

return gson2.toJson(map);
}
 

结果为:


 

把json数据转换为Map

private String nine() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map map = new LinkedHashMap();
map.put("key1", new ToJsonBeanOne(1, "小熊1", 21));
map.put("key2", new ToJsonBeanOne(2, "小熊2", 21));
map.put("key3", new ToJsonBeanOne(3, "小熊3", 21));
String jsonString = gson2.toJson(map);

Type type = new TypeToken>() {}.getType();
Map map2 = gson.fromJson(jsonString, type);

String showString = "";
for (String keyString : map2.keySet()) {
showString += "key:\n" + keyString + "\nvalue:\n" + map2.get(keyString).toString() + "-----\n";
}

showString += "----------------------\n";
return showString;
}
 

结果为:

 

原文链接:https://blog.csdn.net/a249900679/article/details/51386660

json,soscw.com" target="_blank">map --> json

标签:ring   linked   type   ToJson   lin   类型   ble   tty   nbsp   

原文地址:https://www.cnblogs.com/redBack/p/12262473.html


评论


亲,登录后才可以留言!