Analysis of Json data for data transmission over the network
Analysis of Json data for data transmission over the network
In the previous article, we have explained the parsing and serialization of XML files. In this article, we will go on to talk about another data transmission format-Json.
I. Overview
JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. JSON is a language-independent text format that is easy to read and write. It is also easy to parse and generate by machines (generally used to increase the network transmission rate ).
Ii. Syntax Rules
(1) JSON syntax is a subset of the syntax represented by JavaScript objects.
The data in the key-value pairs are separated by commas (,). The curly brackets Save the object square brackets and save the array.
For example:
JSON data is written in the format of name/value pair.
Name/value pair combination names are written in front (in double quotation marks), value pairs are written in the back (also in double quotation marks), separated by colons:
firstName:John
(2) The JSON value can be:
Number (integer or floating point number) string (in double quotation marks) logical value (true or false) array (in square brackets) object (in curly brackets) null 3. Two JSON Structures
JSON has two types of representation structures: objects and arrays.
(1) The object structure starts with braces ({) and ends with braces. The middle part consists of 0 or multiple key (keyword)/value (value) pairs separated by ",". The keywords and values are separated, the syntax structure is like code.
{ key1:value1, key2:value2, ... }
(2) The Array Structure ends. There are 0 or multiple value lists separated by "," in the middle. The syntax structure is like code.
[ { key1:value1, key2:value2 }, { key3:value3, key4:value4 } ]
Next we will learn how to parse Json data. First, download the gson. jar package and import it to Android Studio. Note that you must add the following in the module's gradle file:
compile files('libs/gson-2.3.0.jar')
We need to understand three classes: Gson, JsonObject, and JsonArray.
(1) Gson class:
Gson is a tool class that uses the gson. jar package. It is easy to use and is used to convert Json strings. There are two main methods:
ToJson () fromJson ()
For example:
public static String ObjectToJson(Object object){ Gson gson = new Gson(); return gson.toJson(object); }
These two methods can be used to convert an object to a json string of a key-value pair.
(2) JsonObject class:
A json object is a key that corresponds to a value. braces {} are used, for example, {key: value}
{Author: He honghui, Guan aimin, name: Android source code design mode analysis and practice, price: 79.0}
Common Methods:
(1) constructor:
Public JSONObject (String json) throws JSONException: converts a json String to a JSONObject object public JSONObject (Map copyFrom). A JSONObject object consists of a Map set.
(2) common methods:
Public int length (): return the number of key-value pairs in JSONObject. JSONObject put (String name, **) throws JSONException: Fill in the key-value pairs in JSONObject. Get (): get the key-Value Pair value in JSONArray getJSONArray (String name): get the JSONArray object corresponding to the key in JSONObject.
For example:
JSONObject jsonObject = new JSONObject (); jsonObject. put (author, he honghui, Guan aimin); jsonObject. put (name, Android source code design pattern analysis and practice); jsonObject. put (cost, 79.0 );
(3). JsonArray class:
JsonObject array, which is included in brackets.
[{Author: He honghui, Guan aimin, price: 79.0, name: Android source code design mode analysis and practice}]
Common Methods:
(1) constructor:
JSONArray (): No parameter constructor. generate an object public JSONArray (Collection copyFrom): generate the object public JSONArray (String json) throws JSONException from the Collection: object generated from a json string: Add Value-Added put (int, object) to JSONArray: Add a value at the specified subscript position in jsonarray. Get (int): Specify the subscript to obtain the value.
For example:
JSONArray jsonArray = new JSONArray(); jsonArray.put(jsonObject); Log.d(TAG,jsonArray.toString());
For example, you can try it by yourself. Now let's write a small demo.
JSONObject jsonObject = new JSONObject (); jsonObject. put (author, he honghui, Guan aimin); jsonObject. put (name, Android source code design pattern analysis and practice); jsonObject. put (price, 79.0); JSONArray jsonArray = new JSONArray (); jsonArray. put (jsonObject); jsonArray. put (1314); jsonArray. put (true); Log. d (TAG, jsonArray. toString ());
Input result:
[{Author: He honghui, Guan aimin, price: 79.0, name: Android source code design mode analysis and practice}, 1314, true]
Let's look at the following:
JSONObject jsonObject = new JSONObject (); jsonObject. put (author, he honghui, Guan aimin); jsonObject. put (name, Android source code design pattern analysis and practice); jsonObject. put (price, 79.0); JSONArray jsonArray = new JSONArray (); jsonArray. put (jsonObject); jsonArray. put (1314); jsonArray. put (true); jsonArray. put (1, add at location 1); Log. d (TAG, jsonArray. toString ());
Check the output result:
[{Author: He honghui, Guan aimin, price: 79.0, name: Android source code design pattern analysis and practice}, add at location 1, true]
The comparison results show that the original values are overwritten. You can pay attention to this point. Now, the basic data parsing has been completed based on the xml parsing in the previous article. Please leave a message.