详解Gson使用(四)Map对象转化

项目地址:

Github:https://github.com/smileysx/GsonTest

Oschina:https://git.oschina.net/ysx_xx/GsonText

 

详解Gson使用(一)简单对象转化

http://blog.csdn.net/a249900679/article/details/51385913

详解Gson使用(二)带泛型的List转化

http://blog.csdn.net/a249900679/article/details/51386028

详解Gson使用(三)使用注解

http://blog.csdn.net/a249900679/article/details/51386509

详解Gson使用(四)Map对象转化
http://blog.csdn.net/a249900679/article/details/51386660
详解Gson使用(五)实现百度翻译功能

http://blog.csdn.net/a249900679/article/details/51386727

详解Gson使用(四)Map对象转化_第1张图片

详解Gson使用(四)Map对象转化_第2张图片

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

Map类型的转换就需要用到

 

GsonBuilder().enableComplexMapKeySerialization().create()类生成Gson对象

 

1.      Map类型的转换:

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

 
  1. private String seven() {

  2. Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();

  3. Map map = new HashMap();

  4. map.put("key1", "value1");

  5. map.put("key2", "value2");

  6. map.put("key3", "value3");

  7.  
  8. return gson2.toJson(map);

  9. }

 

结果为:

把json数据转换为Map

 
  1. private String seven() {

  2. Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();

  3. Map map = new HashMap();

  4. map.put("key1", "value1");

  5. map.put("key2", "value2");

  6. map.put("key3", "value3");

  7.  
  8. String jsonString = gson.toJson(map);

  9. Type type = new TypeToken>() {}.getType();

  10. Map map2 = gson2.fromJson(jsonString, type);

  11.  
  12. String showString = "";

  13. for (String keyString : map2.keySet()) {

  14. showString += keyString + ":" + map2.get(keyString) + "\n-----\n";

  15. }

  16.  
  17. showString += "----------------------\n";

  18. return showString;

  19. }

 

结果为:

2.      Map类型的转换:

 

 
  1. private String eight() {

  2. Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();

  3. Map map = new LinkedHashMap();

  4. map.put(new ToJsonBeanOne(1, "小熊1", 21), "value1");

  5. map.put(new ToJsonBeanOne(2, "小熊2", 21), "value2");

  6. map.put(new ToJsonBeanOne(3, "小熊3", 21), "value3");

  7.  
  8. return gson2.toJson(map);

  9. }

 

结果为:

把json数据转换为Map

 
  1. private String eight() {

  2. Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();

  3. Map map = new LinkedHashMap();

  4. map.put(new ToJsonBeanOne(1, "小熊1", 21), "value1");

  5. map.put(new ToJsonBeanOne(2, "小熊2", 21), "value2");

  6. map.put(new ToJsonBeanOne(3, "小熊3", 21), "value3");

  7.  
  8. String jsonString = gson2.toJson(map);

  9.  
  10. Type type = new TypeToken>() {}.getType();

  11. Map map2 = gson.fromJson(jsonString, type);

  12.  
  13. String showString = "";

  14. for (ToJsonBeanOne beanOne : map2.keySet()) {

  15. showString += "key:\n" + beanOne.toString() + "value:" + map2.get(beanOne) + "\n-----\n";

  16. }

  17.  
  18. showString += "----------------------\n";

  19. return showString;

  20. }

结果为:

 

3. Map类型的转换:

 

 
  1. private String nine() {

  2. Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();

  3. Map map = new LinkedHashMap();

  4. map.put("key1", new ToJsonBeanOne(1, "小熊1", 21));

  5. map.put("key2", new ToJsonBeanOne(2, "小熊2", 21));

  6. map.put("key3", new ToJsonBeanOne(3, "小熊3", 21));

  7.  
  8. return gson2.toJson(map);

  9. }

 

 

结果为:

 

把json数据转换为Map

 
  1. private String nine() {

  2. Gson gson2 = new GsonBuilder().enableComplexMapKeySerialization().create();

  3. Map map = new LinkedHashMap();

  4. map.put("key1", new ToJsonBeanOne(1, "小熊1", 21));

  5. map.put("key2", new ToJsonBeanOne(2, "小熊2", 21));

  6. map.put("key3", new ToJsonBeanOne(3, "小熊3", 21));

  7. String jsonString = gson2.toJson(map);

  8.  
  9. Type type = new TypeToken>() {}.getType();

  10. Map map2 = gson.fromJson(jsonString, type);

  11.  
  12. String showString = "";

  13. for (String keyString : map2.keySet()) {

  14. showString += "key:\n" + keyString + "\nvalue:\n" + map2.get(keyString).toString() + "-----\n";

  15. }

  16.  
  17. showString += "----------------------\n";

  18. return showString;

  19. }

 

结果为:

你可能感兴趣的:(个人笔记)