I discovered Google's gson, because I had some understanding of protocolbuf before and had some curiosity, I started using gson.
GitHub homepage: https://github.com/google/gson
After comparison, gson and other existing java json class libraries have the biggest differences. Gson needs to be serialized to achieve entity classes without using annotation to identify fields that need to be serialized. At the same time, gson can flexibly configure fields that need to be serialized by using annotation.
It is very simple to convert List or Map to json:
public String getJsonData(List<?> list) { Gson gson = new Gson(); String jsonstring = gson.toJson(list); return jsonstring;}We will have detailed examples below.
Example
Simple object conversion and List conversion with generics:
Entity Class:
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=" + name + "]"; } }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("Json to simple bean===" + student); // Result: // Simple bean to Json==={"id":1,"name":"Li Kun","birthDay":"Jun 22, 2012 8:27:52 AM"} // Json to simple bean===Student [birthDay=Fri Jun 22 08:27:52 CST 2012, id=1, // name=Li Kun] // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// student2 = new Student(); student2.setId(2); student2.setName("Cao Guisheng"); student2.setBirthDay(new Date()); Student student3 = new Student(); student3.setId(3); student3.setName("Liu Bo"); student3.setBirthDay(new Date()); List<Student> list = new ArrayList<Student>(); list.add(student1); list.add(student2); list.add(student3); System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Result: // List with generics is converted to json==[{"id":1,"name":"Li Kun","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":2,"name":"Cao Guisheng","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":3,"name":"Liu Bo","birthDay":"Jun 22, 2012 8:28:52 AM"}] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=1, name=Li Kun] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=2, name=Cao Guisheng] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=3, name=Liu Bo] } }Execution results:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- PM"},{"id":2,"name":"Cao Guisheng","birthDay":"Jun 22, 2012 9:10:31 PM"},{"id":3,"name":"Liu Bo","birthDay":"Jun 22, 2012 9:10:31 PM"}] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=Li Kun] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=2, name=Cao Guisheng] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=3, name=Liu Bo]