json parsing of Android

Keywords: JSON Android network Google

json parsing of Android

Introduction to json

JSON is one of the data types we often use in network transmission. So what is json?
The full name of json is JavaScript Object Notation. He is a grammar for storing and exchanging text information. It's smaller, faster, and easier to parse than XML
The following three ways of json parsing are introduced: using jsonObject, Gson and fastjson.

Parsing with jsonObject

Use steps
 1. Get the string of json.
2. Get the jsonArray array
 3. Resolve the objects one by one.
    private void parasJSONWithJSONObject(String jsonData) {
        try{
//            Get the jsonArray array
            JSONArray jsonArray = new JSONArray(jsonData);
//            Analyse one by one
            for (int i = 0; i < jsonArray.length();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id = jsonObject.getString("id");
                String name = jsonObject.getString("name");
                Log.d("ok","id is" + id);
                Log.d("ok","name is " + name);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

Using Gson parsing

Gson is an open source library provided by google, and it has powerful functions. However, this library was not added to Android's official API.
So before we do json parsing, we first add Gson's open source package.
The download address is http://download.csdn.net/download/qq_32763643/9898639.
Gson's warehouse address is https://github.com/google/gson

Use steps
    1. Get the json string
    2. Getting Gson objects
    3. Get generic data types through TypeToken and convert them into generic list
    4. Parsing the objects inside
     private void parasJSONWithGSON(String jsonData) {
//        Create gson objects
        Gson gson = new Gson();
//        Get generic data types through TypeToken and convert them into list s
        List<Person> persons =  gson.fromJson(jsonData,new TypeToken<List<Person>>(){ }.getType());
        for (Person person : persons) {
            Log.d("ok","id is "+ person.getId());
            Log.d("ok","name is "+ person.getName());
        }
    }

Using fastjson parsing

fastjson is an open source project currently being carried out by Alibaba. It is very powerful and easy to use. You also need to add packages. His speed is much faster than the previous two methods (about six times faster than gson). Interested partners can learn about it.
The download address of the resource bundle is http://download.csdn.net/download/qq_32763643/9898606
The warehouse address is https://github.com/alibaba/fastjson.

fastjson is simply used here. In order to unify with the above. Do we pass in a string of json?
Use steps
1. Get the json string
2. Convert strings directly into generic list s
3. Analyzing the objects one by one

    private void parasJSONWithFastjson(String jsonData) {

        List<Person> persons = JSON.parseArray(jsonData,Person.class);
        for (Person person : persons) {
            Log.d("ok","id is "+ person.getId());
            Log.d("ok","name is "+ person.getName());
        }
    }

The json file is

    [{"id":"5","name":"pf"},
    {"id":"6","name":"dm"}]

Here's how to pass in parameters

 private void sendRequsetWithHttpURLConnection() {
//        Open threads to initiate network requests
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                String path = "http://10.0.2.2/get_data.json";
                try{
                    URL url = new URL(path);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    InputStream in = connection.getInputStream();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder iresponse = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null){
                        iresponse.append(line);
                    }
                    String response = iresponse.toString();
//                  Here we use fastjson's method
                    parasJSONWithFastjson(response);
                } catch (Exception e){
                    e.printStackTrace();
                } finally {
                    if(connection != null){
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

Posted by dhruvasagar on Thu, 13 Jun 2019 14:30:41 -0700