接下来介绍Map对象与json的相互转换
Map类型的转换就需要用到
GsonBuilder().enableComplexMapKeySerialization().create()类生成Gson对象
1. Map<String, String>类型的转换:
这里直接贴关键代码,其他代码可参考前两篇文章,或下载代码。
private String seven() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
return gson2.toJson(map);
}
结果为:
把json数据转换为Map<String, String>:
private String seven() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map<String, String> map = new HashMap<String, String>();
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
String jsonString = gson.toJson(map);
Type type = new TypeToken<Map<String, String>>() {}.getType();
Map<String, String> map2 = gson2.fromJson(jsonString, type);
String showString = "";
for (String keyString : map2.keySet()) {
showString += keyString + ":" + map2.get(keyString) + "
-----
";
}
showString += "----------------------
";
return showString;
}
结果为:
2. Map<Object, String>类型的转换:
private String eight() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map<ToJsonBeanOne, String> map = new LinkedHashMap<ToJsonBeanOne, String>();
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<Object, String>:
private String eight() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map<ToJsonBeanOne, String> map = new LinkedHashMap<ToJsonBeanOne, String>();
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<Map<ToJsonBeanOne, String>>() {}.getType();
Map<ToJsonBeanOne, String> map2 = gson.fromJson(jsonString, type);
String showString = "";
for (ToJsonBeanOne beanOne : map2.keySet()) {
showString += "key:
" + beanOne.toString() + "value:" + map2.get(beanOne) + "
-----
";
}
showString += "----------------------
";
return showString;
}
结果为:
3. Map<String, Object>类型的转换:
private String nine() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map<String, ToJsonBeanOne> map = new LinkedHashMap<String, ToJsonBeanOne>();
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<String, Object>:
private String nine() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map<String, ToJsonBeanOne> map = new LinkedHashMap<String, ToJsonBeanOne>();
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<Map<String, ToJsonBeanOne>>() {}.getType();
Map<String, ToJsonBeanOne> map2 = gson.fromJson(jsonString, type);
String showString = "";
for (String keyString : map2.keySet()) {
showString += "key:
" + keyString + "
value:
" + map2.get(keyString).toString() + "-----
";
}
showString += "----------------------
";
return showString;
}
结果为:
原文链接:https://blog.csdn.net/a249900679/article/details/51386660