從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}}