Android JSON data parsing


Introduction to this section:

I believe everyone must be familiar with JSON. The data transmission method we generally use when interacting with servers is in the form of Json strings. To save the object, we can also write it as a Json string and store it! Parsing Json I don’t know if you are using Gson, Fastjson, or Jackson Wait, but in this section we will not use these third-party parsing libraries, but use Android's own Json parser to parse Json! Okay, let’s start this section!


1.Json concept and comparison with XML

1) What is Json?

Answer: JavaScript Object Natation, a lightweight data exchange format, like XML, A widely adopted solution for client-server interaction! It has good readability and is easy to write quickly.

2) Comparison between Json and XML:

  • The data readability of JSON and XML is basically the same;
  • JSON and XML also have rich parsing methods
  • Compared to XML, JSON has a small data size
  • The interaction between JSON and JavaScript is more convenient
  • JSON is more convenient for data The descriptiveness of JSON is worse than XML
  • The speed of JSON is much faster than XML

PS: The above comes from Baidu~ To put it simply, the advantages of Json: small size, saving traffic , just not as intuitive and readable as XML Just a little bit worse!

3) Json format specification:

Just like a protocol, there must be a set of specifications. After all, both parties use Json characters String to pass data, the syntax rules are as follows: Data in name/value pairs;Data separated by commas;Curly braces save objects;Square brackets Save array; The writing format of Json data: Name/value pair For example: "person":"coder-pig"For example, a simple Json string:

[
    { "id":"1","name":"基神","age":"18" },
    { "id":"2","name":"B神","age":"18"  },
    { "id":"3","name":"曹神","age":"18" }
]

In addition to parsing Json, we can also splice Json ourselves. Of course, if you spell a Json string yourself and don’t know if it is correct, You can just find a verification tool on Baidu, such as: http://www.php.cn/jsontool Paste the Json string and verify it!


2. The Json parsing class provided by Android

These APIs all exist under the org.json package, and the classes we use There are the following:

  • JSONObject: Json object, which can complete the conversion between Json string and Java object
  • JSONArray: Json array can complete the conversion between Json string and Java collection or object
  • JSONStringer: Json text construction class, this class can help create JSON text quickly and conveniently. Each JSONStringer entity can only create one JSON text
  • JSONTokener: Json parsing class
  • JSONException: Json exception

3. Code example: Parsing Json string:

PS: Here we do not write another servlet or request the website, but directly write Json into the string to parse and simulate Forget it!

1) Simple Json string parsing example:

We are parsing the simple Json above. First, let’s write a POJO class:

Person .java

/**
 * Created by Jay on 2015/9/8 0008.
 */
public class Person {
    private String id;
    private String name;
    private String age;
    public void setId(String id){
        this.id = id;
    }
    public String getId(){
        return this.id;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setAge(String age){
        this.age = age;
    }
    public String getAge(){
        return this.age;
    }
    @Override
    public String toString() {
        return this.name + "~年方:" + this.age;
    }
}

Write a method to parse the above Json string:

private void parseEasyJson(String json){
    persons = new ArrayList();
    try{
        JSONArray jsonArray = new JSONArray(json);
        for(int i = 0;i < jsonArray.length();i++){
            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
            Person person = new Person();
            person.setId(i+"");
            person.setName(jsonObject.getString("name"));
            person.setAge(jsonObject.getString("age"));
            persons.add(person);
        }
    }catch (Exception e){e.printStackTrace();}
}

Running renderings

1.png

Hey, it’s very simple, right? Next, let’s find a more complicated one!


2) Complex Json string parsing example:

What if it is such a Json string?

2.png

Haha, then we need to deduct the data step by step:

The parsing code is as follows:

private void parseDiffJson(String json) {
    try {
        JSONObject jsonObject1 = new JSONObject(json);
        Log.e("Json", json);
        JSONArray jsonArray = jsonObject1.getJSONArray("ch");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = (JSONObject) jsonArray.get(i);
            //取出name
            String sname = jsonObject.getString("names");
            JSONArray jarray1 = jsonObject.getJSONArray("data");
            JSONArray jarray2 = jsonObject.getJSONArray("times");
            Log.e("Json", sname);
            Log.e("Json", jarray1.toString());
            Log.e("Json", jarray2.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

Look at the printed Log:

3.png

Of course there is another layer, if you are interested, you can deduct it yourself...


Summary of this section:

Okay, use the JSON parsing class provided by Android to do it slowly. Of course, you can also parse the process On the other hand, you can splice JSON by yourself. Due to time constraints, we will slowly splice it here, haha. Of course, we will do the advanced part. It’s much easier after learning some third-party Json parsing libraries. ~Okay, that’s it for this section, thank you~