JSON parsing is one of the technologies that Android developers must master. This article is about my understanding and use of JSON parsing.
1. Introduction
When we convert objects to JSON, we name-value them into strings in accordance with certain rules. When you convert a JSON string to an object, use reflection to get the name of each variable in the object (that is, the previous name), and then extract the value of the variable name from the JSON string and assign the value to the variable. In other words, JSON is a solution for transmitting data over the network, and Tojson and Fromjson are codec methods.
2. Use
Native Android supports JSON parsing. To illustrate the problem I created a simple student class:
class Student { public String name; Public int Age ; }
Then I created an instance of student student:
New= "Hsji"= 25;
We can convert the student instance into a JSON string:
Public String Studenttojson (Student Student) { new StringBuilder (); Sb.append ("{\" name\ ": \" "). Append (student.name). append (" \ ", \" age\ ":") . Append (student.age) . Append ("}"); return sb.tostring (); }
The JSON string obtained after invoking the Studenttojson method on the student instance is {"Name": "Hsji", "Age": 25}.
You can also convert the above JSON string to an object instance:
PublicStudent jsontostudent (String json) {Student Student=NewStudent (); Try{jsonobject Jsonobject=NewJsonobject (JSON); Student.name= jsonobject.getstring ("name"); Student.age= Jsonobject.getint ("Age"); returnstudent; } Catch(jsonexception e) {e.printstacktrace (); return NULL; } }
3. Advanced
In the actual work I did not use the method above to process the JSON data, but instead used the "Gson-2.2.4.jar". This is a jar package that Google offers to handle JSON data, which can be easily searched online.
With the introduction of this jar package, our work became easier. The following is also explained by specific use.
or using the student instance above, we'll turn it into a JSON string with the method provided by the JAR package:
New= Gson.tojson (student);
To convert a JSON string into an object:
New== Gson.fromjson (json,student. Class);
With the above example we can see that after using the jar package, it is much easier to process the JSON data.
Next, let's take a look at how to manipulate complex objects:
First create the object class:
classStudent { PublicString name; Public intAge ; } classTeacher { PublicString name; Public BooleanIsheadteacher; } classgroup{ Public intNo; PublicArraylist<student>students; PublicTeacher Teacher; }
We see a group in this class that has a label, a student list, and a teacher.
Here we create an instance of this group:
Group Group =NewGroup (); Group.no= 1; Student Student1=NewStudent (); Student1.name= "Li Lei"; Student1.age= 13; Student Student2=NewStudent (); Student2.name= "Han Meimei"; Student2.age= 13; ArrayList<Student> students =NewArraylist<>(); Students.add (STUDENT1); Students.add (Student2); Group.students=students; Teacher Teacher=NewTeacher (); Teacher.name= "Miss Gao"; Teacher.isheadteacher=false; Group.teacher= teacher;
Then turn it into a JSON string:
String Groupjson = Gson.tojson (group);
Then convert the JSON string into an instance object:
String Groupjson = Gson.tojson (group); = Gson.fromjson (groupjson,group. Class);
We'll add the parsing to the list later!
Android JSON parsing