map --> json
2021-04-20 04:29
标签:ring linked type ToJson lin 类型 ble tty nbsp 接下来介绍Map对象与json的相互转换 Map类型的转换就需要用到 GsonBuilder().enableComplexMapKeySerialization().create()类生成Gson对象 这里直接贴关键代码,其他代码可参考前两篇文章,或下载代码。 private String seven() { 结果为: private String seven() { 结果为: private String eight() { private String eight() { 3. Map 结果为: 把json数据转换为Map private String nine() { 结果为: 原文链接: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
1. Map
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
return gson2.toJson(map);
}
把json数据转换为Map
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map
map.put("key1", "value1");
map.put("key2", "value2");
map.put("key3", "value3");
String jsonString = gson.toJson(map);
Type type = new TypeToken
2. Map
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map
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
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map
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
private String nine() {
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map
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);
}
Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();
Map
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 showString = "";
for (String keyString : map2.keySet()) {
showString += "key:\n" + keyString + "\nvalue:\n" + map2.get(keyString).toString() + "-----\n";
}
showString += "----------------------\n";
return showString;
}