Example of a method that converts the Java list structure to JSON through the Gson library _java

Source: Internet
Author: User
Tags tojson

Discovered Google's Gson, because before for protocolbuf some understanding, with some curiosity, I began to use the Gson.
GitHub Home: Https://github.com/google/gson
By comparison, Gson and other existing Java JSON class libraries have the greatest difference when Gson need to serialize the entity classes do not need to use annotation to identify the fields that need to be serialized, while Gson can flexibly configure the fields that need to be serialized by using annotation.
Converting a list or map to JSON is simple:

Public String Getjsondata (list<?> List) {
  Gson Gson = new Gson ();
  String jsonstring = Gson.tojson (list);
  return jsonstring;
}

We will have a detailed example below.

Example
Simple Object transformation and a list transformation with generics:

Entity classes:

public class Student { 
  private int id; 
  private String name; 
  Private Date birthday; 
 
  public int getId () {return 
    ID; 
  } 
 
  public void setId (int id) { 
    this.id = ID; 
  } 
 
  Public String GetName () {return 
    name; 
  } 
 
  public void SetName (String name) { 
    this.name = name; 
  } 
 
  Public Date Getbirthday () {return 
    birthday; 
  } 
 
  public void Setbirthday (Date birthday) { 
    this.birthday = birthday; 
  } 
 
  @Override public 
  String toString () {return 
    "Student [birthday=] + birthday +", id= "+ ID +", name= " 
        + na" Me + "]"; 
  } 
 
} 

Test class:

Import java.util.ArrayList; 
Import Java.util.Date; 
 
Import java.util.List; 
Import Com.google.gson.Gson; 
 
Import Com.google.gson.reflect.TypeToken; 
 
    public class GsonTest1 {public static void main (string[] args) {Gson Gson = new Gson (); 
    Student student1 = new Student (); 
    Student1.setid (1); 
    Student1.setname ("Li Kun"); 
 
    Student1.setbirthday (New Date ()); System.out.println ("---------- 
    The transformation between simple objects-------------"); 
    The simple bean is converted to json String S1 = Gson.tojson (student1); 
 
    System.out.println ("Simple bean conversion to json===" + S1); 
    JSON is converted to simple bean Student Student = Gson.fromjson (S1, Student.class); 
    System.out.println ("JSON converted to simple bean===" + student); Result://simple Bean converted to json==={"id": 1, "name": "Li Kun", "Birthday": "June, 8:27:52 AM"}//JSON to simple bean===student [b Irthday=fri June 08:27:52 CST, id=1,//name= Li Kun]//////////////////////////////////////////Student Student2 = new Student (); 
    Student2.setid (2); 
    Student2.setname ("Cao Guisheng"); 
 
    Student2.setbirthday (New Date ()); 
    Student Student3 = new Student (); 
    Student3.setid (3); 
    Student3.setname ("Liupo"); 
 
    Student3.setbirthday (New Date ()); 
    list<student> list = new arraylist<student> (); 
    List.add (STUDENT1); 
    List.add (Student2); 
 
    List.add (STUDENT3); 
    System.out.println ("----------transformation-------------between list with generics"); 
    The list with generics is converted to JSON String s2 = gson.tojson (list); 
 
    System.out.println ("Convert a list with generics to json==" + S2);  JSON is converted to a generic List list<student> retlist = Gson.fromjson (s2, New typetoken<list<student>> () 
    {}.gettype ()); 
    for (Student stu:retlist) {System.out.println (STU); //Results:////////////////////////////////////////////////////////////////// Birthday ":" June, 8:28:52 AM "},{" id ": 3,"Name ":" Liupo "," Birthday ":" June, 8:28:52 AM "}//Student [Birthday=fri June 08:28:52 CST, id=1, name= Li Kun] Student [Birthday=fri June 08:28:52 CST, id=2, Name= Cao Guisheng]//Student [Birthday=fri June 08:28:52 CST 2 

 012, id=3, Name= Liupo]}}

Execution results:

----------conversion between simple objects------------- 
simple bean to json==={"id": 1, "name": "Li Kun", "Birthday": "June, 9:10:31 PM"} 
json to Simple bean===student [Birthday=fri June 21:10:31 CST, id=1, name= Li Kun] 
---------- Conversion between list with generics------------- 
with generic list to json==[{"id": 1, "name": "Li Kun", "Birthday": "June, 9:10:31 PM"},{"id": 2 , "name": "Cao Guisheng", "Birthday": "June, 9:10:31 PM"},{"id": 3, "name": "Liupo", "Birthday": "June, 9:10:31 PM"}] 
Student [Birthday=fri June 21:10:31 CST, id=1, Name= Li Kun] 
Student [Birthday=fri June 21:10:31 CST, id=2, n Ame= Cao Guisheng] 
Student [Birthday=fri June 21:10:31 CST, id=3, Name= Liupo] 

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.