GSON實現Java對象的JSON序列化與還原序列化的執行個體教程_java

來源:互聯網
上載者:User

從GitHub下載GSON:https://github.com/google/gson
Gson的應用主要為toJson與fromJson兩個轉換函式,而在使用這種對象轉換之前需先建立好對象的類別以及其成員才能成功的將JSON字串成功轉換成相對應的對象。

 class Examples {  private int answer1 = 100;  private String answer2 = "Hello world!";  Examples(){  }   // default constructor }

序列化JAVA對象成JSON字串

  Examples example1 = new Examples();  Gson gson = new Gson();  String json = gson.toJson(example1);

 json結果將是

{"answer1":100,"answer2":"Hello world!"}

還原序列化JSON字串成對應的JAVA對象

Examples example2= gson.fromJson(json,Examples.class);

==> example2即與example1相同

對象example1通過toJson序列化成JSON字串傳遞,再宣告一個對象example2為接收了JSON後通過fromJson還原序列化成example2,故example1與example2相同


樣本:

import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map;   import com.google.gson.Gson; import com.google.gson.reflect.TypeToken;   class User{   public User(String name,int age,StringBuffer sex,boolean isChild){    this.name = name;    this.age = age;    this.sex = sex;    this.isChild = isChild;   }   private String name;   private int age;   private StringBuffer sex;   private boolean isChild;   public String toString(){    return "{name="+name+";age="+age+";sex="+sex+";isChild="+isChild+"}";   }   public int hashCode(){    return name.hashCode()*100+age;   } }   public class GsonTest {   public static void main(String[] args) {    Gson gson = new Gson();        System.out.println("1普通的Bean的轉換**************************");    System.out.println("將一個Bean轉換成為json字串->");    User user1 = new User("鳳姐",12,new StringBuffer("未知"),true);    System.out.println("轉換之前的user1"+user1);    String json = gson.toJson(user1);    System.out.println("User對象轉換成為Json字串,json==="+json);        System.out.println("***************************");    System.out.println("將一個json字串轉換成為Bean->");    User user2 = gson.fromJson(json, User.class);    System.out.println("轉換成為的user2=="+user2);    System.out.println();        System.out.println("2Collection集合的轉換**************************");    System.out.println("將一個Bean的List集合轉換成為json字串->");    Collection<User> userList1 = new ArrayList<User>();    for(int i=0;i<3;i++){     User user = new User("如花",10+i,new StringBuffer("男"),false);     userList1.add(user);    }    json = gson.toJson(userList1);    System.out.println("User的List集合對象轉換成為Json字串,json==="+json);        System.out.println("***************************");    System.out.println("將一個json字串轉換成為Bean的List集合->");    Collection<User> userList2 =       gson.fromJson(json,          new TypeToken<Collection<User>>(){}.getType());    System.out.println("轉換成為的User的List集合,userList2="+userList2);    System.out.println();        System.out.println("3Array數組的轉換**************************");    System.out.println("將一個Bean的Array數群組轉換成為json字串->");    User [] userArray1 = new User[3];    for(int i=0;i<userArray1.length;i++){     userArray1[i] = new User("芙蓉",20,new StringBuffer("人妖"),true);    }    json = gson.toJson(userArray1);    System.out.println("User的數組對象轉換成為Json字串,json==="+json);        System.out.println("***************************");    System.out.println("將一個json字串轉換成為Bean的數組對象->");    User [] userArray2 = gson.fromJson(json, new TypeToken<User[]>(){}.getType());    System.out.println("轉換成為的User的數組對象,userArray2="+Arrays.toString(userArray2));    System.out.println();        System.out.println("4Map的轉換**************************");    System.out.println("將一個Bean的Map轉換成為json字串->");    Map<String,User> map1 = new HashMap<String,User>();    for(int i=0;i<3;i++){     map1.put(""+(i+10), userArray1[i]);    }    json = gson.toJson(map1);    System.out.println("User的Map集合轉換成為Json字串,json==="+json);        System.out.println("***************************");    System.out.println("將一個json字串轉換成為Bean的數組對象->");    Map<String,User> map2 =       gson.fromJson(json,          new TypeToken<Map<String,User>>(){}.getType());    System.out.println("轉換成為的User的數組對象,map2=="+map2);   } } 

運行結果:

1普通的Bean的轉換************************** 將一個Bean轉換成為json字串-> 轉換之前的user1{name=鳳姐;age=12;sex=未知;isChild=true} User對象轉換成為Json字串,json==={"name":"鳳姐","age":12,"sex":"未知","isChild":true} *************************** 將一個json字串轉換成為Bean-> 轉換成為的user2=={name=鳳姐;age=12;sex=未知;isChild=true}  2Collection集合的轉換************************** 將一個Bean的List集合轉換成為json字串-> User的List集合對象轉換成為Json字串,json===[{"name":"如花","age":10,"sex":"男","isChild":false},{"name":"如花","age":11,"sex":"男","isChild":false},{"name":"如花","age":12,"sex":"男","isChild":false}] *************************** 將一個json字串轉換成為Bean的List集合-> 轉換成為的User的List集合,userList2=[{name=如花;age=10;sex=男;isChild=false}, {name=如花;age=11;sex=男;isChild=false}, {name=如花;age=12;sex=男;isChild=false}]  3Array數組的轉換************************** 將一個Bean的Array數群組轉換成為json字串-> User的數組對象轉換成為Json字串,json===[{"name":"芙蓉","age":20,"sex":"人妖","isChild":true},{"name":"芙蓉","age":20,"sex":"人妖","isChild":true},{"name":"芙蓉","age":20,"sex":"人妖","isChild":true}] *************************** 將一個json字串轉換成為Bean的數組對象-> 轉換成為的User的數組對象,userArray2=[{name=芙蓉;age=20;sex=人妖;isChild=true}, {name=芙蓉;age=20;sex=人妖;isChild=true}, {name=芙蓉;age=20;sex=人妖;isChild=true}]  4Map的轉換************************** 將一個Bean的Map轉換成為json字串-> User的Map集合轉換成為Json字串,json==={"10":{"name":"芙蓉","age":20,"sex":"人妖","isChild":true},"11":{"name":"芙蓉","age":20,"sex":"人妖","isChild":true},"12":{"name":"芙蓉","age":20,"sex":"人妖","isChild":true}} *************************** 將一個json字串轉換成為Bean的數組對象-> 轉換成為的User的數組對象,map2=={10={name=芙蓉;age=20;sex=人妖;isChild=true}, 11={name=芙蓉;age=20;sex=人妖;isChild=true}, 12={name=芙蓉;age=20;sex=人妖;isChild=true}} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.