< excerpt >gson conversion between a Java nested object and a JSON string

Source: Internet
Author: User

JSON (JavaScript Object Notation) is a lightweight data interchange format with good cross-platform features. In recent years, as XML has become a widely used data format in the C/s architecture. For more information on JSON, refer to the following: http://json.org/json-zh.html

Communication between the server and client using the JSON data format often involves conversions between Java objects and JSON strings. In general, we can use some JSON parsing tools, such as: Gson,fastjson, and so on. Of course, we can also manually parse, but it will be more cumbersome.

The following example describes the use of Gson to convert between Java nested objects and JSON strings.

Mainactivity

[Java]View Plaincopy
  1. Package com.example.jsonsample;
  2. Import java.util.ArrayList;
  3. Import android.app.Activity;
  4. Import Android.os.Bundle;
  5. Import Android.view.Menu;
  6. Import Android.widget.TextView;
  7. Import com.example.jsonsample.data.Student;
  8. Import Com.example.jsonsample.data.Subject;
  9. Import Com.google.gson.Gson;
  10. Public class Mainactivity extends Activity {
  11. private TextView Mtextview;
  12. @Override
  13. public void OnCreate (Bundle savedinstancestate) {
  14. super.oncreate (savedinstancestate);
  15. Setcontentview (R.layout.activity_main);
  16. Mtextview = (TextView) Findviewbyid (R.id.mytextview);
  17. Gson Gson = new Gson ();
  18. //Create a Student object
  19. Student originstudent = Getstudent ();
  20. //Convert student objects to JSON string
  21. String reponse = Gson.tojson (originstudent);
  22. //Restore the JSON string to a student object
  23. Student newstudent = Gson.fromjson (reponse, Student.   Class);
  24. Mtextview.settext (reponse);
  25. }
  26. Public Student getstudent () {
  27. Subject sub1 = new Subject ();
  28. Sub1.setsubject_name ("language");
  29. Sub1.setteacher_name ("Miss Zhang");
  30. Subject sub2 = new Subject ();
  31. Sub2.setsubject_name ("mathematics");
  32. Sub2.setteacher_name ("Miss Huang");
  33. Subject sub3 = new Subject ();
  34. Sub3.setsubject_name ("English");
  35. Sub3.setteacher_name ("Mr. Lin");
  36. arraylist<subject> subjects = new arraylist<subject> ();
  37. Subjects.add (SUB1);
  38. Subjects.add (SUB2);
  39. Subjects.add (SUB3);
  40. Student Student = new Student ();
  41. Student.setname ("Yang Hui");
  42. Student.setsubjects (subjects);
  43. return student;
  44. }
  45. @Override
  46. Public Boolean oncreateoptionsmenu (Menu menu) {
  47. Getmenuinflater (). Inflate (R.menu.activity_main, menu);
  48. return true;
  49. }
  50. }


Student

[Java]View Plaincopy
  1. Package com.example.jsonsample.data;
  2. Import java.io.Serializable;
  3. Import java.util.ArrayList;
  4. /**
  5. * Student class, including student name and subject list
  6. *
  7. * @author Yanghui<[email protected]>
  8. */
  9. Public class Student implements Serializable {
  10. /** 
  11. * Serializable
  12. */
  13. private Static final long serialversionuid = -2689979321936117293l;
  14. private String name;
  15. private arraylist<subject> subjects;
  16. /** 
  17. *
  18. * @return Name Student names
  19. */
  20. Public String GetName () {
  21. return name;
  22. }
  23. /** 
  24. *
  25. * @param name Student names
  26. */
  27. public void SetName (String name) {
  28. this.name = name;
  29. }
  30. /** 
  31. *
  32. * @return Subjects Subject List
  33. */
  34. Public arraylist<subject> getsubjects () {
  35. return subjects;
  36. }
  37. /** 
  38. *
  39. * @param subjects Subject List
  40. */
  41. public void Setsubjects (arraylist<subject> subjects) {
  42. this.subjects = subjects;
  43. }
  44. }


Subject

[Java]View Plaincopy
  1. Package com.example.jsonsample.data;
  2. Import java.io.Serializable;
  3. /**
  4. * Subject class, including subject name and subject teacher name
  5. *
  6. * @author Yanghui<[email protected]>
  7. */
  8. Public class Subject implements serializable{
  9. /** 
  10. * Serialversionuid
  11. */
  12. private Static final long serialversionuid = -2574980011831897251l;
  13. private String Subject_name;
  14. private String Teacher_name;
  15. /** 
  16. *
  17. * @return Subject_name Subject Name
  18. */
  19. Public String Getsubject_name () {
  20. return subject_name;
  21. }
  22. /** 
  23. * @param subject_name Subject Name
  24. */
  25. public void Setsubject_name (String subject_name) {
  26. this.subject_name = subject_name;
  27. }
  28. /** 
  29. *
  30. * @return Teacher_name subject Teacher's name
  31. */
  32. Public String Getteacher_name () {
  33. return teacher_name;
  34. }
  35. /** 
  36. *
  37. * @param teacher_name subject Teacher's name
  38. */
  39. public void Setteacher_name (String teacher_name) {
  40. this.teacher_name = teacher_name;
  41. }
  42. }

Picture preview:

< excerpt >gson conversion between a Java nested object and a JSON string

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.