GSON完成Java對象的JSON序列化與反序列化的實例教程。本站提示廣大學習愛好者:(GSON完成Java對象的JSON序列化與反序列化的實例教程)文章只能為提供參考,不一定能成為您想要的結果。以下是GSON完成Java對象的JSON序列化與反序列化的實例教程正文
從GitHub下載GSON:https://github.com/谷歌/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.谷歌.gson.Gson;
import com.谷歌.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}}