Download GSON from GitHub: https://github.com/google/gson
Gson's application mainly includes two conversion functions toJson and fromJson. Before using this object conversion, you need to create the object category and its members to successfully convert the JSON string into the corresponding object.
class Examples { private int answer1 = 100; private String answer2 = "Hello world!"; Examples(){ } // default constructor }Serialize JAVA objects into JSON strings
Examples example1 = new Examples(); Gson gson = new Gson(); String json = gson.toJson(example1);
json result will be
{"answer1":100,"answer2":"Hello world!"}Deserialize JSON strings into corresponding JAVA objects
Examples example2= gson.fromJson(json,Examples.class);
==> example2 is the same as example1
The object example1 is serialized into a JSON string through toJson, and then declares that an object example2 is deserialized into an example2 through fromJson after receiving JSON. Therefore, example1 is the same as example2.
Example:
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("1Conversion of ordinary bean*******************************"); System.out.println("Convert a bean to a json string->"); User user1 = new User("Fengjie",12,new StringBuffer("Unknown"),true); System.out.println("User1 before conversion"+user1); String json = gson.toJson(user1); System.out.println("User object to Json string, json==="+json); System.out.println("***************************"); System.out.println("Convert a json string to Bean->"); User user2 = gson.fromJson(json, User.class); System.out.println("Convert to user2=="+user2); System.out.println(); System.out.println("Convert to 2Collection collection*******************************************"); System.out.println("Convert a Bean's List collection to a json string->"); Collection<User> userList1 = new ArrayList<User>(); for(int i=0;i<3;i++){ User user = new User("Ruhua",10+i,new StringBuffer("Male"),false); userList1.add(user); } json = gson.toJson(userList1); System.out.println("User's List collection object is converted into a Json string, json==="+json); System.out.println("***************************"); System.out.println("Convert a json string to a list collection of beans->"); Collection<User> userList2 = gson.fromJson(json, new TypeToken<Collection<User>>(){}.getType()); System.out.println("Convert to List collection of Users, userList2="+userList2); System.out.println(); System.out.println("Conversion of 3Array array*******************************"); System.out.println("Convert an Array array of Bean into a json string->"); User [] userArray1 = new User[3]; for(int i=0;i<userArray1.length;i++){ userArray1[i] = new User("Hidden",20,new StringBuffer("Shemale"),true); } json = gson.toJson(userArray1); System.out.println("User's array object is converted to a Json string, json==="+json); System.out.println("******************************"); System.out.println("Convert a json string to an array object of Bean->"); User [] userArray2 = gson.fromJson(json, new TypeToken<User[]>(){}.getType()); System.out.println("Convert to the array object of User, userArray2="+Arrays.toString(userArray2)); System.out.println(); System.out.println("Convert 4Map*******************************"); System.out.println("Convert a Bean's Map to a json string->"); 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's Map collection converts to Json strings, json==="+json); System.out.println("***************************"); System.out.println("Convert a json string to an array object of Bean->"); Map<String,User> map2 = gson.fromJson(json, new TypeToken<Map<String,User>>(){}.getType()); System.out.println("Convert to the array object of User, map2=="+map2); } }Running results:
1Conversion of ordinary bean******************************************************* Convert a bean to a json string->User1{name=Fengjie;age=12;sex=unknown;isChild=true} User object is converted to a Json string, json==={"name":"Fengjie","age":12,"sex":"Unknown","isChild":true} ****************************************************** Convert a json string to a bean->User2=={name=Fengjie;age=12;sex=unknown;isChild=true} 2Collection collection conversion************************************************ Convert a Bean's List collection into a json string-> User's List collection object into a Json string, json===[{"name":"Ruhua","age":10,"sex":"Male","isChild":false},{"name":"Ruhua","age":11,"sex":"Male","isChild":false},{"name":"Ruhua","age":12,"sex":"Male","isChild":false}] ****************************************************** Convert a json string to a list collection of beans -> Convert to the List collection of Users, userList2=[{name=Ruhua;age=10;sex=Male;isChild=false}, {name=Ruhua;age=11;sex=Male;isChild=false}, {name=Ruhua;age=12;sex=Male;isChild=false}] 3Array array conversion********************************************************************************* User's array object is converted into a Json string, json===[{"name":"hibi","age":20,"sex":"Shemale","isChild":true},{"name":"Hidden","age":20,"sex":"Shemale","isChild":true},{"name":"Hidden","age":20,"sex":"Shemale","isChild":true}] ******************************************************* Convert a json string to an array object of a bean->Convert to the array object of the User to the user, userArray2=[{name=Hidden;age=20;sex=Hidden; isChild=true}, {name=hibiscus;age=20;sex=hemale;isChild=true}, {name=hibiscus;age=20;sex=hemale;isChild=true}] 4Map conversion************************************************ Convert a Bean's Map to a json string-> User's Map collection is converted into Json string, json==={"10":{"name":"hibi","age":20,"sex":"Shemale","isChild":true},"11":{"name":"hibibi","age":20,"sex":"Shemale","isChild":true},"12":{"name":"hibibi","age":20,"sex":"Shemale","isChild":true}} *************************************************************************** Convert to the array object of User, map2=={10={name=hibi;age=20;sex=hermaid;isChild=true}, 11={name=hibibi;age=20;sex=hermaid;isChild=true}, 12={name=hibibi;age=20;sex=hermaid;isChild=true}}