Google Gson

来源:互联网 发布:网络销售教学 编辑:程序博客网 时间:2024/05/17 06:17

Gson网址http://code.google.com/p/google-gson/


1.简单的处理list和map

Java代码
  1. Gson gson = new Gson();  
  2. List testList = new ArrayList();  
  3. testList.add("first");  
  4. testList.add("second");  
  5. String listToJson = gson.toJson(testList);  
  6. System.out.println(listToJson);       
  7. //prints ["first","second"]  
  8.   
  9. Map testMap = new HashMap();  
  10. testMap.put("id""id.first");  
  11. testMap.put("name","name.second");  
  12. String mapToJson = gson.toJson(testMap);  
  13. System.out.println(mapToJson);    
  14. //prints {"id":"id.first","name":"name.second"}  

 2.处理带泛型的集合

Java代码
  1. List<TestBean> testBeanList = new ArrayList<TestBean>();  
  2. TestBean testBean = new TestBean();  
  3. testBean.setId("id");  
  4. testBean.setName("name");  
  5. testBeanList.add(testBean);  
Java代码
  1. java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {  
  2. }.getType();  
  3. String beanListToJson = gson.toJson(testBeanList,type);  
  4. System.out.println(beanListToJson);  
  5. //prints [{"id":"id","name":"name"}]  
  6.   
  7. List<TestBean> testBeanListFromJson = gson.fromJson(beanListToJson, type);  
  8. System.out.println(testBeanListFromJson);  
  9. //prints [TestBean@1ea5671[id=id,name=name,birthday=<null>]]  

map等其他集合类型同上



3.Date类型转化


先写工具类


Java代码
  1. import java.lang.reflect.Type;  
  2.   
  3. import com.google.gson.JsonDeserializationContext;  
  4. import com.google.gson.JsonDeserializer;  
  5. import com.google.gson.JsonElement;  
  6. import com.google.gson.JsonParseException;  
  7.   
  8. public class UtilDateDeserializer implements JsonDeserializer<java.util.Date> {  
  9.   
  10.     @Override  
  11.     public java.util.Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)  
  12.             throws JsonParseException {  
  13.         return new java.util.Date(json.getAsJsonPrimitive().getAsLong());  
  14.     }  
  15. }  


Java代码
  1. import java.lang.reflect.Type;  
  2.   
  3. import com.google.gson.JsonElement;  
  4. import com.google.gson.JsonPrimitive;  
  5. import com.google.gson.JsonSerializationContext;  
  6. import com.google.gson.JsonSerializer;  
  7.   
  8. public class UtilDateSerializer implements JsonSerializer<java.util.Date> {  
  9.   
  10.     @Override  
  11.     public JsonElement serialize(java.util.Date src, Type typeOfSrc,     
  12.             JsonSerializationContext context) {     
  13.         return new JsonPrimitive(src.getTime());     
  14.     }     
  15.   
  16. }  


Java代码
  1. /** 
  2.      * 序列化方法 
  3.      * @param bean 
  4.      * @param type 
  5.      * @return 
  6.      */  
  7.     public static String bean2json(Object bean, Type type) {  
  8.         Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.classnew UtilDateSerializer())  
  9.                 .setDateFormat(DateFormat.LONG).create();  
  10.         return gson.toJson(bean);  
  11.     }  
  12.   
  13.     /** 
  14.      * 反序列化方法 
  15.      * @param json 
  16.      * @param type 
  17.      * @return 
  18.      */  
  19.     public static <T> T json2bean(String json, Type type) {  
  20.         Gson gson = new GsonBuilder().registerTypeAdapter(java.util.Date.classnew UtilDateDeserializer())  
  21.                 .setDateFormat(DateFormat.LONG).create();  
  22.         return gson.fromJson(json, type);  
  23.     }  



现在开始测试


Java代码
  1. List<TestBean> testBeanList = new ArrayList<TestBean>();  
  2. TestBean testBean = new TestBean();  
  3. testBean.setId("id");  
  4. testBean.setName("name");  
  5. testBean.setBirthday(new java.util.Date());  
  6. testBeanList.add(testBean);  
  7.   
  8. java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<TestBean>>() {  
  9. }.getType();  
  10. String beanListToJson = bean2json(testBeanList, type);  
  11. System.out.println("beanListToJson:" + beanListToJson);  
  12. //prints [{"id":"id","name":"name","birthday":1256531559390}]  
  13.   
  14. List<TestBean> testBeanListFromJson = json2bean(beanListToJson, type);  
  15. System.out.println(testBeanListFromJson);  
  16. //prints [TestBean@77a7f9[id=id,name=name,birthday=Mon Oct 26 12:39:05 CST 2009]]  



后记:对于java.sql.Date的转化同上类似,写两个类用于其序列化和反序列化即可SQLDateDeserializer implements JsonDeserializer<java.sql.Date>

SQLDateSerializer implements JsonSerializer<java.sql.Date>



 对于GsonBuilder详见:

 http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/GsonBuilder.html



*注:TestBean就是一个带id、name、birthday的javabean,就不在这里贴代码了

原创粉丝点击