Gson Example tutorial for implementing JSON serialization and deserialization of Java objects _java

Source: Internet
Author: User
Tags serialization tojson stringbuffer

Download Gson:https://github.com/google/gson from GitHub
The application of Gson is mainly for Tojson and Fromjson two conversion functions, and before using this object conversion, it is necessary to create the class of the object and its members to successfully convert the JSON string to the corresponding object.

 Class Examples {
  private int answer1 =;
  Private String Answer2 = "Hello world!";
  Examples () {
  }   //default constructor
 }

Serializes Java objects into JSON strings

  Examples example1 = new examples ();
  Gson Gson = new Gson ();
  String json = Gson.tojson (example1);

The JSON result will be

{"Answer1": "Answer2": "Hello world!"}

Deserializes the JSON string into the corresponding Java object

Examples example2= Gson.fromjson (json,examples.class);

==> Example2 is the same as example1

The object example1 is passed through the Tojson serialization into a JSON string, and then an object is example2 to example2 after it has been received JSON, so 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 ("1 Ordinary bean conversion **************************"); 
   System.out.println ("Convert a bean into a JSON string->"); 
   User User1 = new User ("Sister Feng", 12,new StringBuffer ("Unknown"), true); System.out.println ("User1 before conversion" +user1);
   String json = Gson.tojson (user1); 
    
   System.out.println ("The user object converts to a 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 ("converted into user2==" +user2); 
    
   System.out.println (); 
   System.out.println ("Conversion of 2Collection sets **************************"); 
   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 ("Like Flowers", 10+i,new stringbuffer ("male"), false); 
   Userlist1.add (user); 
   JSON = Gson.tojson (USERLIST1); 
    
   System.out.println ("User's list collection object converts to a JSON string, json===" +json); 
   System.out.println ("***************************"); 
   System.out.println ("Convert a JSON string into a bean's list collection->"); collection<user> UserList2 = Gson.fromjson (JSON, new typetoken<collection<user>> () {}.GEttype ()); 
   System.out.println ("The list set of the converted user, userlist2=" +userlist2); 
    
   System.out.println (); 
   System.out.println ("Conversion of 3Array array **************************"); 
   System.out.println ("Convert the array array of a bean into a JSON string->"); 
   User [] userArray1 = new User[3]; 
   for (int i=0;i<userarray1.length;i++) {Userarray1[i] = new User ("Hibiscus", 20,new stringbuffer ("Simon"), true); 
   JSON = Gson.tojson (userArray1); 
    
   System.out.println ("User's array object converts to a JSON string, json===" +json); 
   System.out.println ("***************************"); 
   System.out.println ("Convert a JSON string into an array object for the Bean->"); 
   User [] userArray2 = Gson.fromjson (JSON, new typetoken<user[]> () {}.gettype ()); 
   System.out.println ("The array object of the converted user, userarray2=" +arrays.tostring (userArray2)); 
    
   System.out.println (); 
   System.out.println ("4Map conversion **************************"); 
   System.out.println ("Convert the map of a bean into 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 string, json===" +json); 
   System.out.println ("***************************"); 
   System.out.println ("Convert a JSON string into an array object for the Bean->"); Map<string,user> MAP2 = Gson.fromjson (JSON, new typetoken<map<string,user>> () {}.gettype 
   ()); 
  System.out.println ("The array object of the converted user, map2==" +MAP2); 
 } 
}

Run Result:

1 Normal bean conversions ************************** a bean into a JSON string-> before the conversion of User1{name= Fengjie; age=12;sex= unknown; Ischild=true} 
The user object converts to a JSON string, json==={"name": "Sister Feng", "Age": "Sex": "Unknown", "IsChild": true} *************************** Converts a JSON string into a bean-> converted into user2=={name= Fengjie; age=12;sex= unknown; ischild=true} 2Collection set conversion *********************** Converts a bean's list collection to a JSON string-> the user's list collection object into a JSON string, json===[{"name": "Flower", "age": "Sex": "Male", "IsChild": false},{"name": "Flower", "age": One, "sex": "Male", "IsChild": false},{"name": "Like Flowers", "age": One, "sex": "Male", "IsChild": false}] * * * * * * Converts a JSON string into a bean's list collection-> the list of the user to convert to, userlist2=[{name= as a flower; age=10;sex=; ischild= False}, {name= as flowers; age=11;sex= male; Ischild=false}, {name= as flower; age=12;sex= male; Ischild=false}] 3Array array conversion ****************** Converts a bean's array array to a JSON string-> user's array object converts to a JSON string, json===[{"name": "Hibiscus", "Age": "Sex": "Simon", "IsChild" : true},{"name": "Hibiscus", "Age": "Sex": "Simon", "IsChild": true},{"name": "Hibiscus", "Age": "Sex": "Simon", "IsChild":true}] *************************** a JSON string into the bean's array object-> converted to the user's array object, Userarray2=[{name= Furong; age=20;sex= Ischild=true}, {name= furong; age=20;sex= Simon; Ischild=true}, {name= furong; age=20;sex= Simon; Ischild=true}] 4Map conversion ************** Converts the map of a bean into a JSON string-> user's map collection into a JSON string, json==={"ten": {"name": "Hibiscus", "Age": "Sex": "Simon", " IsChild ": true}," one ": {" name ":" Hibiscus "," Age ":" Sex ":" Simon "," IsChild ": true}," a ": {" name ":" Hibiscus "," Age ":", "" Sex ":" Simon "," IsChild ': true}} *************************** converts a JSON string into a bean's array object-> an array object for the converted user, map2=={10={name= Furong; age=20 
 sex= Simon Ischild=true}, 11={name= Furong; age=20;sex= transvestite; Ischild=true}, 12={name= Furong; age=20;sex= Simon; Ischild=true}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.