android中Gson解析

最近在做Android项目,用到了Gson解析,总结一下放在这里,方便以后使用

 

服务器端代码:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.sdufe.domain;  
  2.   
  3. /** 
  4.  * @author lili.guo 
  5.  *  
  6.  *         2014-6-10下午2:20:31 
  7.  */  
  8. public class People {  
  9.   
  10.     private int id;  
  11.     private String username;  
  12.     private String address;  
  13.   
  14.     /** 
  15.      * 无参构造函数 
  16.      */  
  17.     public People() {  
  18.         // TODO Auto-generated constructor stub  
  19.     }  
  20.   
  21.     public People(int id, String username, String address) {  
  22.         this.id = id;  
  23.         this.username = username;  
  24.         this.address = address;  
  25.     }  
  26.   
  27.     public int getId() {  
  28.         return id;  
  29.     }  
  30.   
  31.     public void setId(int id) {  
  32.         this.id = id;  
  33.     }  
  34.   
  35.     public String getUsername() {  
  36.         return username;  
  37.     }  
  38.   
  39.     public void setUsername(String username) {  
  40.         this.username = username;  
  41.     }  
  42.   
  43.     public String getAddress() {  
  44.         return address;  
  45.     }  
  46.   
  47.     public void setAddress(String address) {  
  48.         this.address = address;  
  49.     }  
  50.   
  51. }  

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.sdufe.tools;  
  2.   
  3. import com.google.gson.Gson;  
  4.   
  5. /** 
  6.  * @author lili.guo 
  7.  * 
  8.  * 2014-6-10下午2:39:47 
  9.  */  
  10. public class JsonTools {  
  11.       
  12.     /** 
  13.      * 将普通的字符串转化为json 
  14.      * @param value 
  15.      * @return 
  16.      */  
  17.     public static String CreateJsonStringbyGson(String value){  
  18.         Gson gson=new Gson();  
  19.         String json=gson.toJson(value);  
  20.         return json;  
  21.     }  
  22.   
  23. }  

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.sdufe.Service;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import com.sdufe.domain.People;  
  9.   
  10. /** 
  11.  * @author lili.guo 
  12.  * 
  13.  * 2014-6-10下午2:27:36 
  14.  */  
  15. public class JsonService {  
  16.       
  17.     public People getPeople(){  
  18.         People people=new People(1001, "Thea", "beijing");  
  19.         return people;  
  20.     }  
  21.       
  22.     public List<People> getList(){  
  23.         List<People> list=new ArrayList<People>();  
  24.         list.add(new People(1001, "Thea", "beijing"));  
  25.         list.add(new People(1001, "Thea", "beijing"));  
  26.         list.add(new People(1001, "Thea", "beijing"));  
  27.         return list;  
  28.     }  
  29.       
  30.     public List<String> getString(){  
  31.         List<String> list=new ArrayList<String>();  
  32.         list.add("beijing");  
  33.         list.add("shanghai");  
  34.         return list;  
  35.     }  
  36.       
  37.     public List<Map<String, Object>> getListMap(){  
  38.         List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();  
  39.         Map<String, Object> map1=new HashMap<String, Object>();  
  40.         map1.put("id", 1001);  
  41.         map1.put("username", "Thea");  
  42.         map1.put("address", "beijing");  
  43.         list.add(map1);  
  44.         Map<String, Object> map2=new HashMap<String, Object>();  
  45.         map2.put("id", 1001);  
  46.         map2.put("username", "Thea");  
  47.         map2.put("address", "beijing");  
  48.         list.add(map2);  
  49.         return list;  
  50.     }  
  51.   
  52. }  


其中jsonService中转化的数据类型比较多,差不多经常用到的几种都放在这里了

 

 

客户端代码:

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.sdufe.tools;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import com.google.gson.Gson;  
  8. import com.google.gson.reflect.TypeToken;  
  9. import com.sdufe.domain.People;  
  10.   
  11. /** 
  12.  * @author lili.guo 
  13.  *  
  14.  *         2014-6-10下午2:53:16 
  15.  */  
  16. public class GsonTool {  
  17.   
  18.     public static People getPeople(String jsonString) {  
  19.   
  20.         Gson gson = new Gson();  
  21.         People people = gson.fromJson(jsonString, People.class);  
  22.         return people;  
  23.     }  
  24.   
  25.     public static List<People> getList(String jsonString) {  
  26.   
  27.         Gson gson = new Gson();  
  28.         List<People> list=new ArrayList<People>();  
  29.         list=gson.fromJson(jsonString, new TypeToken<List<People>>() {  
  30.         }.getType());  
  31.         return list;  
  32.     }  
  33.       
  34.     public static List<String> getString(String json){  
  35.           
  36.         Gson gson=new Gson();  
  37.         List<String> list=new ArrayList<String>();  
  38.         list=gson.fromJson(json, new TypeToken<List<String>>() {  
  39.         }.getType());  
  40.         return list;  
  41.     }  
  42.       
  43.     public List<Map<String, Object>> getListMap(String json){  
  44.         List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();  
  45.         Gson gson=new Gson();  
  46.         list=gson.fromJson(json, new TypeToken<List<Map<String,Object>>>() {  
  47.         }.getType());  
  48.         return list;  
  49.     }  
  50.   
  51. }  


稍微解释一下

 

服务器端负责把String类型的字符串转化成json类型,而客户端则负责把json类型解析成普通的String类型

posted @ 2016-11-24 19:25  天涯海角路  阅读(64)  评论(0)    收藏  举报