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
- Package com.example.jsonsample;
- Import java.util.ArrayList;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.view.Menu;
- Import Android.widget.TextView;
- Import com.example.jsonsample.data.Student;
- Import Com.example.jsonsample.data.Subject;
- Import Com.google.gson.Gson;
- Public class Mainactivity extends Activity {
- private TextView Mtextview;
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Mtextview = (TextView) Findviewbyid (R.id.mytextview);
- Gson Gson = new Gson ();
- //Create a Student object
- Student originstudent = Getstudent ();
- //Convert student objects to JSON string
- String reponse = Gson.tojson (originstudent);
- //Restore the JSON string to a student object
- Student newstudent = Gson.fromjson (reponse, Student. Class);
- Mtextview.settext (reponse);
- }
- Public Student getstudent () {
- Subject sub1 = new Subject ();
- Sub1.setsubject_name ("language");
- Sub1.setteacher_name ("Miss Zhang");
- Subject sub2 = new Subject ();
- Sub2.setsubject_name ("mathematics");
- Sub2.setteacher_name ("Miss Huang");
- Subject sub3 = new Subject ();
- Sub3.setsubject_name ("English");
- Sub3.setteacher_name ("Mr. Lin");
- arraylist<subject> subjects = new arraylist<subject> ();
- Subjects.add (SUB1);
- Subjects.add (SUB2);
- Subjects.add (SUB3);
- Student Student = new Student ();
- Student.setname ("Yang Hui");
- Student.setsubjects (subjects);
- return student;
- }
- @Override
- Public Boolean oncreateoptionsmenu (Menu menu) {
- Getmenuinflater (). Inflate (R.menu.activity_main, menu);
- return true;
- }
- }
Student
[Java]View Plaincopy
- Package com.example.jsonsample.data;
- Import java.io.Serializable;
- Import java.util.ArrayList;
- /**
- * Student class, including student name and subject list
- *
- * @author Yanghui<[email protected]>
- */
- Public class Student implements Serializable {
- /**
- * Serializable
- */
- private Static final long serialversionuid = -2689979321936117293l;
- private String name;
- private arraylist<subject> subjects;
- /**
- *
- * @return Name Student names
- */
- Public String GetName () {
- return name;
- }
- /**
- *
- * @param name Student names
- */
- public void SetName (String name) {
- this.name = name;
- }
- /**
- *
- * @return Subjects Subject List
- */
- Public arraylist<subject> getsubjects () {
- return subjects;
- }
- /**
- *
- * @param subjects Subject List
- */
- public void Setsubjects (arraylist<subject> subjects) {
- this.subjects = subjects;
- }
- }
Subject
[Java]View Plaincopy
- Package com.example.jsonsample.data;
- Import java.io.Serializable;
- /**
- * Subject class, including subject name and subject teacher name
- *
- * @author Yanghui<[email protected]>
- */
- Public class Subject implements serializable{
- /**
- * Serialversionuid
- */
- private Static final long serialversionuid = -2574980011831897251l;
- private String Subject_name;
- private String Teacher_name;
- /**
- *
- * @return Subject_name Subject Name
- */
- Public String Getsubject_name () {
- return subject_name;
- }
- /**
- * @param subject_name Subject Name
- */
- public void Setsubject_name (String subject_name) {
- this.subject_name = subject_name;
- }
- /**
- *
- * @return Teacher_name subject Teacher's name
- */
- Public String Getteacher_name () {
- return teacher_name;
- }
- /**
- *
- * @param teacher_name subject Teacher's name
- */
- public void Setteacher_name (String teacher_name) {
- this.teacher_name = teacher_name;
- }
- }
Picture preview:
< excerpt >gson conversion between a Java nested object and a JSON string