Gson is a Java class library provided by Google for mapping between Java objects and JSON data. You can turn a JSON string into a Java object, or vice versa.
Jar and Source: http://code.google.com/p/google-gson/downloads/list
Entity class:
[Java]View Plaincopy
[Java]View Plaincopy
- 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:
[HTML]View Plaincopy
- 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 ("Kun");
- Student1.setbirthday (New Date ());
- // //////////////////////////////////////////////////////////
- System.out.println ("----------conversions between simple objects-------------");
- Simple Bean to JSON
- String S1 = Gson.tojson (student1);
- System.out.println ("Simple bean conversion to json===" + S1);
- JSON to Simple bean
- Student Student = Gson.fromjson (s1, Student.class);
- SYSTEM.OUT.PRINTLN ("JSON to Simple bean===" + student);
- Results:
- Simple Bean converted to json==={"id": 1, "name": "Kun", "BirthDay": "June," 8:27:52 AM "}
- JSON converted to simple bean===student [birthday=Fri June 08:27:52 CST, id=1,
- name= 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 ("Sharon");
- Student3.setbirthday (New Date ());
- List<Student> list = new ArrayList<Student> ();
- List.add (STUDENT1);
- List.add (Student2);
- List.add (STUDENT3);
- System.out.println ("-----------------------of conversions between lists with generics");
- List with generics converted to JSON
- String s2 = Gson.tojson (list);
- System.out.println ("list with generics converted to json==" + s2);
- JSON to a list with generics
- List<Student> retlist = Gson.fromjson (s2,
- New TypeToken<List<Student>> () {
- }.gettype ());
- for (Student stu:retlist) {
- System.out.println (Stu);
- }
- Results:
- List with generics converted to json==[{"id": 1, "name": "Kun", "BirthDay": "June," 8:28:52 AM "},{" id ": 2," name ":" Cao Guisheng "," BirthDay ": "June, 8:28:52 AM"},{"id": 3, "name": "Sharon", "BirthDay": June, 8:28:52 AM "}]
- Student [birthday=Fri June 08:28:52 CST, id=1, name= kun]
- Student [birthday=Fri June 08:28:52 CST, id=2, name= Cao Guisheng]
- Student [birthday=Fri June 08:28:52 CST, id=3, name= Sharon]
- }
- }
Execution Result:
[Plain]View Plaincopy
- ----------conversions between simple objects-------------
- Simple Bean converted to json==={"id": 1, "name": "Kun", "BirthDay": "June," 9:10:31 PM "}
- JSON converted to Simple bean===student [Birthday=fri June 21:10:31 CST, id=1, Name= Kun]
- ----------conversions between lists with generics-------------
- List with generics converted to json==[{"id": 1, "name": "Kun", "BirthDay": "June," "9:10:31 PM"},{"id": 2, "name": "Cao Guisheng", "BirthDay": "June 22 , 9:10:31 PM "},{" id ": 3," "Name": "Sharon", "BirthDay": "June," 9:10:31 pm "}]
- Student [Birthday=fri June 21:10:31 CST, id=1, Name= Kun]
- Student [Birthday=fri June 21:10:31 CST, id=2, name= Cao Guisheng]
- Student [Birthday=fri June 21:10:31 CST, id=3, name= Sharon]
JSON conversion Tool Gson example one-Simple object conversion and list conversion with generics (http://blog.csdn.net/lk_blog/article/details/7685169)
JSON conversion Tool Gson Example two-gson annotations and Gsonbuilder (http://blog.csdn.net/lk_blog/article/details/7685190)
JSON conversion Tool Gson example of three-map processing (upper) (http://blog.csdn.net/lk_blog/article/details/7685210)
JSON conversion Tool Gson example four-map processing (bottom) (http://blog.csdn.net/lk_blog/article/details/7685224)
JSON conversion Tool Gson example five-special requirements processing in real development (http://blog.csdn.net/lk_blog/article/details/7685237)
JSON conversion Tool Gson example six-register Typeadapter and process enum type (http://blog.csdn.net/lk_blog/article/details/7685347)
Instance code download: http://download.csdn.net/detail/lk_blog/4387822
JSON conversion Tool Gson example one-Simple object conversions and list conversions with generics