gson常用用法小结(转载)

转载地址:http://jackyrong.iteye.com/blog/1915344

gson是来自google的十分不错的json转换器,转换起来十分方便,今天小结下其中的一些用法: 

1) 最基本的对象和json相互转换 
POJO: 
 

Java代码  收藏代码
  1. public class ModelObject {  
  2.   String name;  
  3.   int val;  
  4.   boolean status;  
  5.   double f;  
  6.     
  7.   public ModelObject(String name, int val,   
  8.   boolean status, double f) {  
  9.     super();  
  10.     this.name = name;  
  11.     this.val = val;  
  12.     this.status = status;  
  13.     this.f = f;  
  14.   }  
  15.     
  16.   @Override  
  17.   public String toString() {  
  18.     return "ModelObject [name=" + name + ",  
  19.     val=" + val + ", status="  
  20.     + status + ", f=" + f + "]";  
  21.   }  
  22.    



  相互转换的代码: 

Java代码  收藏代码
  1. final Gson gson = new Gson();  
  2. // original object instantiation  
  3. ModelObject modelObject = new ModelObject("myname", 12, true, 2.3);  
  4. System.out.println("toJson ---");  
  5. System.out.println("Original Java object : " + modelObject);  
  6. // converting an object to json object  
  7. String json = gson.toJson(modelObject);  
  8. System.out.println("Converted JSON string is : " + json);  
  9.    
  10. System.out.println("fromJson----");  
  11. // getting object from json representation  
  12. System.out.println("Original JSON string is : " + json);  
  13. // converting json to object  
  14. ModelObject modelObject1 = gson.fromJson(json, ModelObject.class);  
  15. System.out.println("Converted Java object : " + modelObject1);  


输出如下: 

toJson --- 
Original Java object : ModelObject [name=myname, val=12, status=true, f=2.3] 
Converted JSON string is : {"name":"myname","val":12,"status":true,"f":2.3} 
fromJson---- 
Original JSON string is : {"name":"myname","val":12,"status":true,"f":2.3} 
Converted Java object : ModelObject [name=myname, val=12, status=true, f=2.3] 



2) 针对泛型 
   先看一个泛型的例子定义 

 

Java代码  收藏代码
  1. public class GenericModel<T> {  
  2.   T value;  
  3.     
  4.   public GenericModel(T value) {  
  5.     super();  
  6.     this.value = value;  
  7.   }  
  8.     
  9.   @Override  
  10.   public String toString() {  
  11.     return "Model2 [value=" + value + "]";  
  12.   }  
  13. }  


   然后转换方法如下: 
Gson gson = new Gson(); 

System.out.println("A generic object demo"); 
// a generified object 
GenericModel<Integer> model = new GenericModel<>(12); 

// converting to json representation 
String json = gson.toJson(model); 
System.out.println("json representation :" + json); 

// converting back to object 
Type collectionType = new TypeToken<GenericModel<Integer>>() { 
}.getType(); 
GenericModel<Integer> modelObj = 
                  gson.fromJson(json, collectionType); 
System.out.println("converted object representation: " + modelObj); 

System.out.println("\nA object from collection framework\n"); 
// for collection framework objects 
List<String> listOfString = new ArrayList<>(); 
listOfString.add("ajduke"); 
listOfString.add("ajduchess"); 

// conversion to json 
String jsonStr = gson.toJson(listOfString); 
System.out.println("json representation :" + jsonStr); 

Type collectionType2 = new TypeToken<List<String>>() { 
}.getType(); 
List<String> listObj = gson.fromJson(jsonStr, collectionType2); 
System.out.println("converted object representation: " + listObj); 

输出: 
A generic object demo 
json representation :{"value":12} 
converted object representation: Model2 [value=12] 

A object from collection framework 

json representation :["ajduke","ajduchess"] 
converted object representation: [ajduke, ajduchess] 

也就是说,GSON把json转换为JAVA泛型对象的时候,要先定义好 
Type collectionType = new TypeToken<GenericModel<Integer>>() { 
}.getType() 
告诉到底用什么类型,然后再用fromjson方法 

3) 使用transient指定不需要转换为json的属性 

 

Java代码  收藏代码
  1. public class ModelObject {  
  2.   String name;  
  3.   transient int val;  
  4.   boolean status;  
  5.   double f;  
  6.     
  7.   public ModelObject(String name, int val,   
  8.    boolean status, double f) {  
  9.     super();  
  10.     this.name = name;  
  11.     this.val = val;  
  12.     this.status = status;  
  13.     this.f = f;  
  14.   }  
  15.     
  16.   @Override  
  17.   public String toString() {  
  18.     return "ModelObject [name=" + name + ",  
  19.     val=" + val + ", status="  
  20.     + status + ", f=" + f + "]";  
  21.   }  
  22. }  


  则转换程序: 

Java代码  收藏代码
  1. Gson gson = new Gson();  
  2. // original object  
  3. ModelObject modelObject = new ModelObject("namesake", 50, true, 4.3);  
  4. System.out.print("Original Java object : ");  
  5. System.out.println(modelObject);  
  6.    
  7. // converting to an json representation  
  8. String json = gson.toJson(modelObject);  
  9. System.out.print("Converted JSON string is : ");  
  10. System.out.println(json);  
  11.    
  12. // getting back the object from json representation  
  13. ModelObject modelObject3 = gson.fromJson(json, ModelObject.class);  
  14. System.out.print("Converted Java object : ");  
  15. System.out.println(modelObject3);  



  则输出的信息中,就不会转换val了. 

4) 也可以使用注解,指定哪些是要暴露转换的属性,如: 
 

Java代码  收藏代码
  1. @Expose  
  2. private Integer businessId;  



但这个时候要用
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation() 
.create(); 
BusinessSystem bus = (BusinessSystem) (gson.fromJson(data, 
BusinessSystem.class)); 

这样类似的方式转换; 

posted @ 2016-01-04 20:05  青青子衿J  阅读(197)  评论(0)    收藏  举报