JSON data analysis of Android handy notes 44

JSON (JavaScript object notation) is a lightweight data exchange format, which adopts a text format completely independent of language, and provides an ideal data exchange format for web application development.

This article will mainly introduce how to create JSON data on the server side and how to parse JSON data on the Android client side in Android development.

1. JSON data structure

There are two data structures in JSON: objects and arrays.

1.1 object

In JSON, an object starts with "{" (left parenthesis) and ends with "}" (right parenthesis). Each "name" is followed by a ":" (colon), followed by the value of the name. Multiple "names: values" are separated by "," (comma). The name needs to be enclosed in double quotation marks. If the value is a string, it must be enclosed in double quotation marks. If it is a numeric type, it is not required. The structural diagram is shown in Figure 1.

Figure 1 Schematic diagram of JSON object structure

The following code is a simple JSON object example:

1.2 array

In JSON, an array is an ordered collection of values. An array starts with "[" (left bracket) and ends with "]" (right bracket). Values are separated by "," (comma). Its structural diagram is shown in Figure 2.

Figure 2 Schematic diagram of JSON array structure

The following code is a simple JSON array example:

["Beijing", "Shanghai", "Guangzhou"]

1.3 type of value

In the object and array structure of JSON, the value value can not only be simple data types such as numbers and strings, but also objects and arrays, as shown in Figure 3.

Figure 3 types of JSON values

Therefore, we can use the combination of objects and arrays to form complex data structures. The following code uses the object structure to define a "students" object. The "students" object contains a student array, and the value in the student array is a JSON object.

2. Generate JSON data on the server side

Generally, when the client requests the server data, the server can send the data to the client in the form of XML document, JSON data or HTML.

So how to generate JSON data on the server side? First, the following two preparations need to be completed.

(1) we need to create a web project using MyEclipse. Here, I name the project "jsondemoproject" to simulate server-side Web services.

(2) we also need to import JSON API package json-lib-2.2.2-jdk15.jar into the project.

In the JSON API, a jsonobject class is provided. By calling the put (object key, object value) method of the jsonobject class, an object object can be stored in the jsonobject object as a key value pair. By calling the toString () method of the jsonobject class, you can convert the jsonobject object into JSON data.

The following code creates a jsontools class and implements the static method createjsonstring() to generate JSON data.

By using this method, we can easily transfer various data in and convert it into JSON data. For example, we can implement a simple method to obtain the list of person objects in the jsonservice class, as follows:

The person object has three attributes: ID (int), name (string), and age (int).

Finally, we can 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:

As you can see, in the dopost () method, we get the person object list listperson by calling the getlistperson () method and pass it into the jsontools. Createjsonstring () method to get a string of JSON data.

Publish the project to Tomcat and use the browser to access the web project. You can see the interface shown in Figure 4. The person object list is successfully converted into JSON data.

Figure 4 generated JSON data

3. Parse JSON data on the client

Through the above steps, we have generated JSON data on the server. It is easy to obtain the JSON data in our Android project. We only need to use the httpurlconnection interface provided by Android to access the URL shown in Figure 4.

So, after obtaining the JSON data on the server, how to complete the parsing of the JSON data in the Android project?

By observing the JSON data shown in Figure 4, we can see that:

(1) the outermost layer of the JSON data is jsonobject, the key of jsonobject is "persons", and the value is a jsonarray.

(2) three jsonobject objects are included in jsonarray.

(3) in each embedded jsonobject object, there is a combination of three key value pairs.

After analyzing the form of JSON data, you can start parsing it. In the Android project, we can create a jsontools class and implement the getlistperson () class method to parse the JSON data obtained from the server and restore it to the person object list. specific

The code is as follows:

In this example, click the button button to send a request for obtaining JSON data to the server. After obtaining JSON data from the server, you can 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 5.

Figure 5 operation results

The above content is the full description of JSON data analysis in Android handy note 44 shared by you. I hope you like it.

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
分享
二维码
< <上一篇
下一篇>>