android parses Json using FastJson, Gson, JSON -- Exercise

Keywords: JSON Java xml Android

One. Introduction of Google GSON Technology

Gson, a Java class library, can convert Java objects into JSON or JSON strings into an equivalent Java object.

Gson supports any complex Java object including objects without source code.

Two. Introduction of Alibaba FastJson Technology

FastJson is a Json processing toolkit, which includes "serialization" and "deserialization". It has the following characteristics:
The fastest, test shows that fastjson has very fast performance, beyond any other Java Json parser. Including Jack Json, who claims to be the fastest.
Powerful, fully support Java Bean, Collection, Map, Date, Enum, support paradigm, support introspection.
Dependency-free, able to run directly in Java SE version 5.0 or above
Support Android.
Open Source (Apache 2.0)

Three. JSON Vs XML
1. Data readability of JSON and XML is basically the same
2.JSON and XML also have rich parsing tools.
3.JSON is smaller than XML.
4. The interaction between JSON and JavaScript is more convenient
5.JSON is less descriptive of data than XML
6.JSON is much faster than XML

Four. json parsing class provided by Android 2.3
The JSON parsing part of android is under package org.json. There are mainly the following categories:

JSONObject: It can be seen as a JSON object, which is the basic unit of the system about the definition of JSON, which contains a pair of key / value values. Its response to External (value output by applying toString() method) calls is embodied in a standard string (e.g. {"JSON": "Hello, World"}, the outermost braces wrapped, where Key and Value are coloned:"separated). It has a slight format for internal behavior, such as initializing a JSONObject instance and adding values by referring to the internal put() method: new JSONObject().put("JSON", "Hello, World!"), separated by commas between Key and Value. Value types include: Boolean, JSONArray, JSONObject, Number, String, or the default JSONObject.NULL object.

JSONStringer: JSON text construction class, according to official interpretation, this class can help create JSON text quickly and conveniently. Its greatest advantage is that it can reduce program exceptions caused by format errors. Referring to this class can automatically and strictly create JSON text in accordance with JSON syntax rules. Each JSONStringer entity can only create a JSON text. Its greatest advantage is that it can reduce program exceptions caused by format errors. Referring to this class can automatically and strictly create JSON text in accordance with JSON syntax rules. Each JSONStringer entity can only create a JSON text correspondingly.

JSONArray: It represents an ordered set of values. Convert it to String output (toString) in the form of square brackets wrapped with commas for values. "Separation (e.g. [value1,value2,value3], you can personally use short code to understand its format more intuitively." The inner part of this class also has query behavior. Both get() and opt() methods can return the specified value through index, and put() method can be used to add or replace the value. Also, the value types of this class can include: Boolean, JSONArray, JSONObject, Number, String, or the default JSONObject.NULL object.

JSONTokener: json parsing class
Exceptions used in JSONException: json

The above theoretical sources: http://www.zhixing123.cn/android/39134.html

The steps and packages that need to be imported are not provided, only the test code.
First, the Java side imports Json's jar package to convert the data into a JSON string:

//Converting object sets into Json
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("clazz", "150831");
        jsonObject.put("lists",fqs.size());

        JSONArray jsonArray=new JSONArray();

        for (FQ fq:fqs) {
            JSONObject object=new JSONObject();
            object.put("name", fq.getName());
            object.put("content", fq.getContent());
            object.put("time", fq.getTime());
            jsonArray.add(object);
        }

        jsonObject.put("fqs", jsonArray);

        // Store the collection of objects in the request domain
        ServletActionContext.getRequest().setAttribute("fqs", jsonObject.toString());

Get data JSON type characters:

//Define network data path
        String path=getString(R.string.server_name)+"fqActiongetJson.action";
        try {
            URL url=new URL(path);
            //Open connection
            HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(5000);//overtime
            if(httpURLConnection.getResponseCode()==200){
                InputStream is=httpURLConnection.getInputStream();
                //test data
                BufferedReader br=new BufferedReader(new InputStreamReader(is));
                StringBuffer sb=new StringBuffer();
                String str=null;
                while((str=br.readLine())!=null){
                    sb.append(str);
                }
                Log.i("hhhhh",sb.toString());

JSON parsing

try {
                    JSONObject jsonObject=new JSONObject(sb.toString());
                    String clazz=jsonObject.getString("class");
                    int num=jsonObject.getInt("lists");
                    JSONArray jsonArray=jsonObject.getJSONArray("fqs");
                    for (int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject1=jsonArray.getJSONObject(i);
                        String name=jsonObject1.getString("name");
                        String content=jsonObject1.getString("content");
                        String time=jsonObject1.getString("time");
                        FQ fq=new FQ(name,content,time);
                        lists.add(fq);
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

//Gson: Need a guide

     Gson gson=new Gson();
                   BigFQ bigFQ=gson.fromJson(stringBuffer.toString(),BigFQ.class);

                   String clazz=bigFQ.getClazz();
                   int num=bigFQ.getLists();

                   Log.i("test","clazz "+clazz+" num:"+num);
                   lists.addAll(bigFQ.getFqs());

//FastJson

  BigFQ bigFQ=JSON.parseObject(stringBuffer.toString(),BigFQ.class);
                    String clazz=bigFQ.getClazz();
                    int num=bigFQ.getLists();

                    Log.i("test","clazz "+clazz+" num:"+num);
                    lists.addAll(bigFQ.getFqs());

Posted by tinuviel on Wed, 12 Dec 2018 04:33:07 -0800