Gson是Google的開源項目(項目主頁:https://code.google.com/p/google-gson/),
Gson在線幫助文檔:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html
Gson包下載地址:https://google-gson.googlecode.com/files/google-gson-2.2.4-release.zip
示例:
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}}
參考文章:
http://qindongliang1922.iteye.com/blog/1915710
http://www.cnblogs.com/happyPawpaw/archive/2012/10/23/2735697.html