The definition of JSON:
A lightweight data interchange format with good readability and fast-coding features. The industry's mainstream technology provides it with a complete solution (a bit like regular expressions that are supported by most languages today), allowing data exchange between platforms. JSON uses a highly compatible text format, as well as behavior similar to the C language system.
JSON object:
Objects in JSON (object) begin with "{" and End With "}". Each item in the object is a Key-value pair, represented in the form of "Key:value", separated by commas between the key-value pairs. such as: {"name": "Coolxing", "Age" =24, "male": true, "address": {"Street": "Huilongguan", "City": "Beijing", "Country": "The" }}. The JSON object's key can only be of type string, and value can be string, number, false, True, null, object objects, or even array arrays, which means nested situations can exist.
JSON array:
The JSON array (array) ends with "[" and "]", and each element in the array can be string, number, false, True, null, object, or even array, and the elements between the arrays are separated by commas. such as ["coolxing", "," "Street": "Huilongguan", "City": "Beijing", "Country": "the".
1. Foreword
JSON data is a common data format for Android network development. There are several ways to parse JSON data.
1.1 Use the official band Jsonobject
1.2 Use of Third-party open source libraries, including but not limited to Gson, Fastjson, Jackson, this article mainly introduces the use of Gson libraries provided by Google.
How to use 2.JSONObject
2.1 Sample Code
Org.json.JSONArray;
Org.json.JSONObject;
private void Parsejsonwithjsonobject (String jsondata) {
try {
//Jsondata the JSON string into a JSON array, that is, Jsonarray
// Jsondata can be read from a file, or it can be obtained from the server
jsonarray Jsonarray = new Jsonarray (jsondata);
for (int i = 0; i< jsonarray.length (); i++) {
//loop traversal, remove Jsonobject object in turn/
//Use Getint and GetString method to remove corresponding key values
Jsonobject Jsonobject = Jsonarray.getjsonobject (i);
int stu_no = Jsonobject.getint ("Stu_no");
String stu_name = jsonobject.getstring ("Stu_name");
String stu_sex = jsonobject.getstring ("Stu_sex");
LOG.D ("Mainactivity", "Stu_no:" + stu_no);
LOG.D ("Mainactivity", "Stu_name:" + stu_name);
LOG.D ("Mainactivity", "Stu_sex:" + stu_sex);
}
catch (Exception e) {
e.printstacktrace ();
}
}
The 2.2 string jsondata is as follows, and the diagram shows the results
[{"Stu_no": 12345, "Stu_name": "John", "stu_sex": "Male"},{"Stu_no"
: 12346, "Stu_name": "Tom", "stu_sex": "Male"
},{"Stu_no": 12347, "Stu_name": "Lily", "Stu_sex": "Female"}]
How to use 3.GSON
3.1 Download and install
• Copy downloaded Gson-2.6.1.jar to the project directory->app->libs folder
3.2 Method Introduction
Tojson (PARAMS1), converting incoming objects to strings
Fromjson (PARAMS1,PARAMS2), passing in two parameters, converts the string params1 to the data type specified by PARAMS2.
3.3 Sample Code
3.3.1 The resolution of a single object
public class Student {
private int stu_no;
Private String stu_name;
Private String stu_sex;
Student (int stu_no,string stu_name,string stu_sex) {
this.stu_no = stu_no;
This.stu_name = Stu_name;
This.stu_sex = Stu_sex;
}
serialization, converts the Student object Stu to a string str
Student stu = new Student (123, "Tom", "male");
Gson Gson = new Gson ();
String str = Gson.tojson (stu);
Deserializes, converts a string to a student object
jsondata = "{\ stu_no\": 12345,\ "stu_name\": \ "john\", \ "stu_sex\": \ "Male\"} ";
Gson Gson = new Gson ();
Parsing of 3.3.2 JSON arrays (native classes)
Gson Gson = new Gson ();
Int[] INTs = {1, 2, 3, 4, 5};
String[] strings = {"abc", "Def", "Ghi"};
Serialization (serialization)
//converting an array of integers to a JSON array
Gson.tojson (ints);//==> [1,2,3,4,5]
//Converting a string array to a JSON array
Gson.tojson (strings); ==> ["abc", "Def", "Ghi"]
//deserialization (deserialization)
//Convert JSON array to native class array
//Ints2, string2 and INTs, Strings equal
int[] ints2 = Gson.fromjson ("[1,2,3,4,5]", int[].class);
Parsing of 3.3.3 JSON arrays (custom classes)
For jsondata similar to 2.2, contains 3 student objects//is
different from the native class, and requires TypeToken to obtain the desired resolution data type
///The following code is run Students contains three student objects
Gson Gson = new Gson ();
List<student> students;
3.4 More methods
The simplicity of Gson is that it can automatically map strings to native or custom objects, eliminating the need to write code for parsing.
Gson more ways to read Gson's usage on GitHub, readme.md-> User Guide.
The above content for you to introduce the Android Gson, Jsonobject parsing json method, I hope to help.