Gson parsing JSON of Android learning notes 45

JSON, namely JavaScript object naming, is a lightweight data exchange format. It adopts a text format completely independent of language, which provides an ideal data exchange format for web application development.

JSON object:

In JSON, objects begin with "{" and end with "}". Each item in an object is a key value pair in the form of "key: value". Key value pairs are separated by commas. For example: {"name": "coolxing", "age" = 24, "male": true, "address": {"street": "Huilongguan", "city": "Beijing", "country": "China"}}. The key of a JSON object can only be of type string, and value can be of type string, Number, false, true, null, object object or even array, that is, nesting can exist

JSON array:

JSON arrays start with "[" and end with "]". Each element in the array can be a string, and the elements between arrays are separated by commas. For example ["coolxing", 24, {"street": "Huilongguan", "country": "China"}]

In the previous blog post "Android learning notes 44: JSON data parsing", we used the basic JSON API to create JSON data on the server side and parse JSON data on the Android client side.

In fact, to create and parse JSON data, you can also use gson. Gson is a Java class library provided by Google to map between Java objects and JSON data. Using gson, you can easily convert a string of JSON data into a Java object, or a Java object into the corresponding JSON data.

1. Two important methods of gson

In the API of gson, two important methods are provided: tojson () and fromjson (). The tojson () method is used to convert Java objects into corresponding JSON data, and the fromjson () method is used to convert JSON data into corresponding Java objects.

1.1 tojason() method

The tojson () method is used to convert Java objects into corresponding JSON data, mainly in the following forms:

  (1)String toJson(JsonElement jsonElement);

  (2)String toJson(Object src);

  (3)String toJson(Object src,Type typeOfSrc);

The method (1) is used to convert the jsonelement object (which can be jsonobject, jsonarray, etc.) into JSON data; Method (2) is used to serialize the specified object object into corresponding JSON data; Method (3) is used to serialize the specified object object (which can include generic types) into corresponding JSON data.

1.2 fromjason() method

The fromjson () method is used to convert JSON data into corresponding Java objects, mainly in the following forms:

  (1)<T> T fromJson(JsonElement json,Class<T> classOfT);

  (2)<T> T fromJson(JsonElement json,Type typeOfT);

  (3)<T> T fromJson(JsonReader reader,Type typeOfT);

  (4)<T> T fromJson(Reader reader,Class<T> classOfT);

  (5)<T> T fromJson(Reader reader,Type typeOfT);

  (6)<T> T fromJson(String json,Class<T> classOfT);

  (7)<T> T fromJson(String json,Type typeOfT);

The above methods are used to parse different forms of JSON data into Java objects.

2. Generate JSON data on the server side

To generate JSON data on the server side using gson technology, you need to complete the following two preparations.

(1) I created a web project using MyEclipse. Here, I named the project "gsondemoproject" to simulate server-side Web services.

(2) import gson API package gson-2.2.1.jar into the project.

Then, we can create a jsontools tool class in the project and implement the static method createjsonstring (), in which JSON data is generated by using gson technology. The specific implementation of this method is as follows.

You can see that the specific implementation of this method is very simple. First, create a gson object, and then call the tojson () method of the gson object to convert the passed value (any Java object) into a JSON string.

By using this method, we can easily pass in any Java object and convert it into JSON data. As in the previous blog post, we can implement a simple method to obtain the list of person objects in the jsonservice class, as follows:

In this method, we add three person objects to the list. Each person object has three attributes: ID (int), name (string) and age (int).

Finally, we need to create a jsonaction class inherited from httpservlet and implement the dopost () method to respond to the client's request to the server. The details are as follows:

In this method, we obtain the person object list listperson by calling the getlistperson() method in the jsonservice class, and pass it into the jsontools. Createjsonstring() method to generate the JSON data of the person object list. Publish the project to Tomcat and use the browser to access the web project. You can see the interface shown in Figure 1. The person object list is successfully converted into JSON data.

Figure 1 generated JSON data

3. Parse JSON data on the client

In the Android project, we can access the URL shown in Figure 1 through the httpurlconnection interface to obtain JSON data on the server.

After getting the JSON data, you can restore the JSON data shown in Figure 1 to the corresponding person object list by using the fromjson () method mentioned earlier. Of course, because gson is used here, you also need to import the gson-2.2.1.jar package into the Android project. The specific implementation method is as follows.

As you can see, the code implementation of parsing JSON data using gson is also very simple. Among them, typetoken is the data type converter provided by gson, which supports the type conversion of multiple data sets. Its reflection mechanism can map the parsed Java objects to the corresponding data sets.

In this example, also click the button button to send a request for obtaining JSON data to the server. After obtaining JSON data from the server, use the above code to complete the parsing of JSON data. Finally, the parsed person objects are displayed in the textview control in turn. The result of program running is shown in Figure 2.

Figure 2 operation results

The above content is all the narration of gson parsing JSON in Android learning note 45, which I hope you like.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>