Gson解析(3)——Map處理數據(上)

Map的存儲結構式Key/Value形式,Key 和 Value可以是普通類型,也可以是自己寫的JavaBean(本文),還可以是帶有泛型的List(下一篇博客).本例中您要重點看如何將Json轉回爲普通JavaBean對象時TypeToken的定義.

實體類:

[java] view plaincopy
  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. }  

測試類:

[java] view plaincopy
  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. }  

結果:

[plain] view plaincopy
  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]  


文章轉自:http://blog.csdn.net/lk_blog/article/details/7685210

感謝博主的無私分享!


發佈了68 篇原創文章 · 獲贊 53 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章