Android series — an example of JSON data parsing

The previous essay introduces three XML data formats transmitted from the server in detail. For the server, the data formats returned to the client are generally divided into HTML, XML and JSON. Then this essay will explain the knowledge of JSON, including how to parse our JSON data through JSON lib and gson, And how to parse JSON data from the server on our Android client and update it to the UI.

1、 What is JSON

JSON (JavaScript object notation) is a lightweight data exchange format. Compared with XML, parsing XML is more complex and requires writing a large section of code, so the data exchange formats of client and server are often exchanged through JSON. Especially for web development, JSON data format can be parsed directly through JavaScript on the client.

JSON has two data structures. One is an unordered jsonobject object in the form of (key / value) pairs. An object starts with "{" (left curly bracket) and ends with "}" (right curly bracket). Each "name" is followed by a ":" (colon); "," (comma) is used to separate the 'name / value' pairs.

For example: {"name": "Xiaoluo"}, this is the simplest JSON object. For this data format, the key value must be of string type, while for value, it can be of string, number, object, array and other data types:

Another data format is an ordered set of values. This form is called jsonarray, and an array is an ordered set of values. An array starts with "[" (left bracket) and ends with "]" (right bracket). Values are separated by "," (comma).

2、 Parsing JSON data format

Here, we will use two JSON parsing libraries to parse our JSON data format and generate our JSON data format.

1.json-lib( http://json-lib.sourceforge.net/ )

To use JSON lib for parsing, we need to introduce a third-party package, because JSON lib is divided into two versions, one for jdk1.3 and the other for JDK1.5. Here we download the JSON lib package of JDK1.5, in which several other jar packages need to be introduced:

The two most commonly used classes are jsonobject and jsonarray, which represent JSON objects and JSON arrays respectively. These two classes implement the JSON interface. Let's see how to convert several common data formats into our JSON objects through a few small examples (we generally call it JSON data serialization) And then convert the JSON object into our data format (called deserialization).

① Simple java bean serialization and deserialization

First, we define a simple JavaBean object, then convert a person object into a JSON object, and then reverse sequence the JSON object into our person object.

First, we define a jsontools class. This class has two static methods. We can get a JSON type string object and a JSON object through these two methods

We can use jsonobject directly. Jsonobject = new jsonobject(); This method can get a JSON object, and then add a key / value pair to our JSON object through the element () or put () method. Let's take a look at the first example to realize a simple conversion between person object and JSON object

Let's look at the console output:

{"person": {"address": "Guangzhou", "Id": 1, "name": "Xiaoluo"}}

The curly braces on the outside are a JSON object with a pair of key / value inside. The {"address": "Guangzhou", "name": "Xiaoluo"} inside is the JSON string object we converted into

Let's take a look at how to convert JSON objects into our bean objects

② Convert objects of type list < person >

③ List < map < string, string > > type JSON object conversion

Through the above example, we can understand how to realize the mutual conversion of JavaBean, list, map and JSON data through the JSON lib parsing library

2.gson( http://code.google.com/p/google-gson/ )

Let's take a look at the JSON parsing library provided by Google. Similarly, we need to download the jar package gson and import it into our project

Using gson, we can easily realize the mutual conversion of data objects and JSON objects. Among them, two methods are most commonly used. One is fromjson(), which converts JSON objects into the data objects we need, and the other is tojson(), which converts our data objects into JSON objects. Let's also take a comprehensive example to see how gson is used:

Look at the console output:

Person: {"Id": 1, "name": "Xiaoluo", "address": "Guangzhou"} person [id = 1, address = Guangzhou] ---------------------------------------- persons: [{"Id": 1, "address": "Guangzhou"}, {"Id": 2, "name": "Android", "address": "Shanghai"}] [person [id = 1, address = Shanghai] ------------------------------------- string ---- > ["Guangzhou", "Shanghai", "Beijing"] List2 ---- > [Guangzhou, Shanghai, Beijing] --------------------------------------- map ---- > [{ID ":" 001 "," name ":" Android "}] listmap2 ---- > [{id = 001, age = 20, name = Xiaoluo}, age = 33, name = Android}]------------------------------------------------

3、 Parsing JSON data on the server side in the Android client

Let's complete a comprehensive example. The Android client requests some data from the server through an asynctask asynchronous task, and then updates the data content to our spinner UI control after parsing these data.

Let's first look at the server-side code:

If the parameter requested by the client is type = JSON, the response will give the client a JSON data format

Next, let's look at the client code. First, let's look at the layout file of the client, which is actually a button and a spinner control. After clicking the button, we request the server-side data through the HTTP protocol, and then update the data of our spinner control after receiving it

Write a class to parse JSON data format on Android client:

Of course, our httputils class is also indispensable:

Finally, let's take a look at our mainactivity class:

Of course not. Open our network authorization

Finally, let's take a look at the renderings:

In this way, we have completed the data exchange between the client and the server through JSON

Summary: This essay mainly explains the concept of JSON, a lightweight data exchange format, and two parsing classes (JSON lib and gson) for parsing JSON data. Finally, through a small example, it realizes the data exchange using JSON on the Android client and server.

Original link: http://www.cnblogs.com/xiaoluo501395377/p/3446605.html The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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