`

Json转换利器Gson之实例三-Map处理(上)

阅读更多

Map的存储结构式Key/Value形式,Key 和 Value可以是普通类型,也可以是自己写的JavaBean(本文),还可以是带有泛型的List(下一篇博客).本例中您要重点看如何将Json转回为普通JavaBean对象时TypeToken的定义.

 

实体类:

 

 

 

  1. public class Point {  
  2.     private int x;  
  3.     private int y;  
  4.   
  5.     public Point(int x, int y) {  
  6.         this.x = x;  
  7.         this.y = y;  
  8.     }  
  9.   
  10.     public int getX() {  
  11.         return x;  
  12.     }  
  13.   
  14.     public void setX(int x) {  
  15.         this.x = x;  
  16.     }  
  17.   
  18.     public int getY() {  
  19.         return y;  
  20.     }  
  21.   
  22.     public void setY(int y) {  
  23.         this.y = y;  
  24.     }  
  25.   
  26.     @Override  
  27.     public String toString() {  
  28.         return "Point [x=" + x + ", y=" + y + "]";  
  29.     }  
  30.   
  31. }  


测试类:

 

 

  1. import java.util.LinkedHashMap;  
  2. import java.util.Map;  
  3.   
  4. import com.google.gson.Gson;  
  5. import com.google.gson.GsonBuilder;  
  6. import com.google.gson.reflect.TypeToken;  
  7.   
  8. public class GsonTest3 {  
  9.   
  10.     public static void main(String[] args) {  
  11.         Gson gson = new GsonBuilder().enableComplexMapKeySerialization()  
  12.                 .create();  
  13.   
  14.         Map<Point, String> map1 = new LinkedHashMap<Point, String>();// 使用LinkedHashMap将结果按先进先出顺序排列  
  15.         map1.put(new Point(56), "a");  
  16.         map1.put(new Point(88), "b");  
  17.         String s = gson.toJson(map1);  
  18.         System.out.println(s);// 结果:[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]  
  19.   
  20.         Map<Point, String> retMap = gson.fromJson(s,  
  21.                 new TypeToken<Map<Point, String>>() {  
  22.                 }.getType());  
  23.         for (Point p : retMap.keySet()) {  
  24.             System.out.println("key:" + p + " values:" + retMap.get(p));  
  25.         }  
  26.         System.out.println(retMap);  
  27.   
  28.         System.out.println("----------------------------------");  
  29.         Map<String, Point> map2 = new LinkedHashMap<String, Point>();  
  30.         map2.put("a"new Point(34));  
  31.         map2.put("b"new Point(56));  
  32.         String s2 = gson.toJson(map2);  
  33.         System.out.println(s2);  
  34.   
  35.         Map<String, Point> retMap2 = gson.fromJson(s2,  
  36.                 new TypeToken<Map<String, Point>>() {  
  37.                 }.getType());  
  38.         for (String key : retMap2.keySet()) {  
  39.             System.out.println("key:" + key + " values:" + retMap2.get(key));  
  40.         }  
  41.   
  42.     }  
  43. }  


结果:

 

 

  1. [[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]  
  2. key:Point [x=5, y=6] values:a  
  3. key:Point [x=8, y=8] values:b  
  4. {Point [x=5, y=6]=a, Point [x=8, y=8]=b}  
  5. ----------------------------------  
  6. {"a":{"x":3,"y":4},"b":{"x":5,"y":6}}  
  7. key:a values:Point [x=3, y=4]  
  8. key:b values:Point [x=5, y=6]  

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics