JSON conversion Tool Gson example one-Simple object conversions and list conversions with generics

Source: Internet
Author: User
Tags tojson

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
  1. Public class Student {
  2. private int id;
  3. private String name;
  4. private Date BirthDay;
  5. public int getId () {
  6. return ID;
  7. }
  8. public void setId (int id) {
  9. this.id = ID;
  10. }
  11. Public String GetName () {
  12. return name;
  13. }
  14. public void SetName (String name) {
  15. this.name = name;
  16. }
  17. Public Date Getbirthday () {
  18. return BirthDay;
  19. }
  20. public void Setbirthday (Date birthDay) {
  21. this.birthday = BirthDay;
  22. }
  23. @Override
  24. Public String toString () {
  25. return "Student [birthday=" + BirthDay + ", id=" + ID + ", name="
  26. + name + "]";
  27. }
  28. }


Test class:

[HTML]View Plaincopy
  1. Import java.util.ArrayList;
  2. Import Java.util.Date;
  3. Import java.util.List;
  4. Import Com.google.gson.Gson;
  5. Import Com.google.gson.reflect.TypeToken;
  6. public class GsonTest1 {
  7. public static void Main (string[] args) {
  8. Gson Gson = new Gson ();
  9. Student student1 = new Student ();
  10. Student1.setid (1);
  11. Student1.setname ("Kun");
  12. Student1.setbirthday (New Date ());
  13. // //////////////////////////////////////////////////////////
  14. System.out.println ("----------conversions between simple objects-------------");
  15. Simple Bean to JSON
  16. String S1 = Gson.tojson (student1);
  17. System.out.println ("Simple bean conversion to json===" + S1);
  18. JSON to Simple bean
  19. Student Student = Gson.fromjson (s1, Student.class);
  20. SYSTEM.OUT.PRINTLN ("JSON to Simple bean===" + student);
  21. Results:
  22. Simple Bean converted to json==={"id": 1, "name": "Kun", "BirthDay": "June," 8:27:52 AM "}
  23. JSON converted to simple bean===student [birthday=Fri June 08:27:52 CST, id=1,
  24. name= Kun]
  25. // //////////////////////////////////////////////////////////
  26. Student student2 = new Student ();
  27. Student2.setid (2);
  28. Student2.setname ("Cao Guisheng");
  29. Student2.setbirthday (New Date ());
  30. Student student3 = new Student ();
  31. Student3.setid (3);
  32. Student3.setname ("Sharon");
  33. Student3.setbirthday (New Date ());
  34. List<Student> list = new ArrayList<Student> ();
  35. List.add (STUDENT1);
  36. List.add (Student2);
  37. List.add (STUDENT3);
  38. System.out.println ("-----------------------of conversions between lists with generics");
  39. List with generics converted to JSON
  40. String s2 = Gson.tojson (list);
  41. System.out.println ("list with generics converted to json==" + s2);
  42. JSON to a list with generics
  43. List<Student> retlist = Gson.fromjson (s2,
  44. New TypeToken<List<Student>> () {
  45. }.gettype ());
  46. for (Student stu:retlist) {
  47. System.out.println (Stu);
  48. }
  49. Results:
  50. 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 "}]
  51. Student [birthday=Fri June 08:28:52 CST, id=1, name= kun]
  52. Student [birthday=Fri June 08:28:52 CST, id=2, name= Cao Guisheng]
  53. Student [birthday=Fri June 08:28:52 CST, id=3, name= Sharon]
  54. }
  55. }


Execution Result:

[Plain]View Plaincopy
  1. ----------conversions between simple objects-------------
  2. Simple Bean converted to json==={"id": 1, "name": "Kun", "BirthDay": "June," 9:10:31 PM "}
  3. JSON converted to Simple bean===student [Birthday=fri June 21:10:31 CST, id=1, Name= Kun]
  4. ----------conversions between lists with generics-------------
  5. 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 "}]
  6. Student [Birthday=fri June 21:10:31 CST, id=1, Name= Kun]
  7. Student [Birthday=fri June 21:10:31 CST, id=2, name= Cao Guisheng]
  8. 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

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.