json parsing used in headline projects

Posted by agent_smith_sp on Fri, 28 Jun 2019 00:59:27 +0200

In the project, the json format data returned from the json parsing website is used. My network access uses the retrofit 2.0 framework, which contains the json converter to object converter. Of course, you can customize the converter yourself.
In fact, the converter is based on the most basic json string text to constantly parse and transform, encapsulated, so we had better understand the most basic conversion method.
My data is aggregated data:
https://www.juhe.cn/box/index/id/235
For example:

{
    "reason":"Successful return",
    "result":{
        "stat":"1",
        "data":[
            {
                "uniquekey":"9acb533ba0f54242743fa9128a4d6ab3",
                "title":"Don't be afraid of any country! There is absolutely no pressure for China to engage in territorial defense operations.",
                "date":"2017-05-26 16:30",
                "category":"Military",
                "author_name":"Sabre",
                "url":"http:\/\/mini.eastday.com\/mobile\/170526163040904.html",
                "thumbnail_pic_s":"http:\/\/08.imgmini.eastday.com\/mobile\/20170526\/20170526163040_8c883638d210ad10b70cd17679f2f3b3_2_mwpm_03200403.jpeg",
                "thumbnail_pic_s02":"http:\/\/08.imgmini.eastday.com\/mobile\/20170526\/20170526163040_8c883638d210ad10b70cd17679f2f3b3_3_mwpm_03200403.jpeg",
                "thumbnail_pic_s03":"http:\/\/08.imgmini.eastday.com\/mobile\/20170526\/20170526163040_8c883638d210ad10b70cd17679f2f3b3_4_mwpm_03200403.jpeg"
            },
            {
                "uniquekey":"9dfb9216a9767ed36665f778eb2cdfb2",
                "title":"Video Warsaw Co., Ltd. of the same side won the most dynamic small and medium-sized enterprises Award in Poland",
                "date":"2017-05-26 16:20",
                "category":"Military",
                "author_name":"China Networks",
                "url":"http:\/\/mini.eastday.com\/mobile\/170526162000174.html",
                "thumbnail_pic_s":"http:\/\/06.imgmini.eastday.com\/mobile\/20170526\/20170526162000_d100909589f10e7972b6715a43a0a66a_1_mwpm_03200403.jpeg"
            },
            {
                "uniquekey":"30a6ccf5ec03c3166b737b25f369457a",
                "title":"Putin's two "navies" are so powerful that the cat carried by the Navy on its way to the battlefield is not easy.",
                "date":"2017-05-26 16:16",
                "category":"Military",
                "author_name":"Military Illustration",
                "url":"http:\/\/mini.eastday.com\/mobile\/170526161634427.html",
                "thumbnail_pic_s":"http:\/\/07.imgmini.eastday.com\/mobile\/20170526\/20170526161634_233fd32f75522e6f80b6eee7cbba6ad0_3_mwpm_03200403.jpeg",
                "thumbnail_pic_s02":"http:\/\/07.imgmini.eastday.com\/mobile\/20170526\/20170526161634_233fd32f75522e6f80b6eee7cbba6ad0_4_mwpm_03200403.jpeg",
                "thumbnail_pic_s03":"http:\/\/07.imgmini.eastday.com\/mobile\/20170526\/20170526161634_233fd32f75522e6f80b6eee7cbba6ad0_2_mwpm_03200403.jpeg"
            }
        ]
    },
    "error_code":0
}

Usually the json format returned and this type, more complex than all kinds of nested...
What we need to do is to analyse and transform layer by layer.

My code access uses retrofit:

{
        Log.d("lihui", "List<Data> list---" + list);
        //1

        Retrofit retrofit = new Retrofit.Builder().
                baseUrl("http://v.juhe.cn/").
                addConverterFactory(GsonConverterFactory.create()).build();

        //2
        HttpService myService = retrofit.create(HttpService.class);
        //3
        retrofit.Call<ResponseBody> call = myService.getData(type, "9f3097f4cbe47e8abb01ca3b92e49cda");
        //4
        call.enqueue(new Callback<ResponseBody>() {

            @Override
            public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
                Log.d("lihui", "123onResponse");
                try {
                    ResponseBody httpResult = response.body();
                    //Start with raw data
                    String result = httpResult.string();
                    org.json.JSONObject jsonObject = new org.json.JSONObject(result);
                    String reason = jsonObject.getString("reason");
                    ToastUtils.setToastText(context, reason);
                    String result2 = jsonObject.getString("result");
                    org.json.JSONObject jsonObject2 = new org.json.JSONObject(result2);
                    String stat = jsonObject2.getString("stat");
                    String data = jsonObject2.getString("data");
                    org.json.JSONArray jsonArray2 = new org.json.JSONArray(data);
                    list.clear();
                    for (int i = 0; i < jsonArray2.length(); i++) {
                        //Get each JsonObject object
                        org.json.JSONObject myjObject = jsonArray2.getJSONObject(i);
                        if (myjObject != null) {
                            Data data1 = new Data(myjObject);
                            Log.d("lihui", "Fragment onResponse getUniquekey---" + data1.getUniquekey());
                            Log.d("lihui", "Fragment onResponse data---" + data1);
                            list.add(data1);
                        }
                    }
                    if (list != null && list.size() > 0 && mHandler != null) {
                        Message msg = mHandler.obtainMessage();
                        msg.what = 0;
                        msg.obj = list;
                        mHandler.sendMessage(msg);
                        empty.setVisibility(View.GONE);
                        cacheMap.put(type, list);
                    }
                    Log.d("lihui", "159 list---" + list);

                } catch (Exception e) {
                    Log.d("lihui", "114e---" + e.getMessage());
                    pullToRefreshRecyclerView.onRefreshComplete();
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d("lihui", "165t:" + t.getMessage());
                t.printStackTrace();
                Message msg = mHandler.obtainMessage();
                msg.what = 1;
                msg.obj = type;
                mHandler.sendMessage(msg);
            }

        });
        return list;
    }

1. Get the most original data, and the server returns {xxxx} directly.
Convert to a jsonObject

 org.json.JSONObject: This is api Self-contained
                    org.json.JSONObject jsonObject = new org.json.JSONObject(result);

2. Get the second level object result2 and convert it to jsonObject.

       String reason = jsonObject.getString("reason");
                    ToastUtils.setToastText(context, reason);
                    String result2 = jsonObject.getString("result");
                                        org.json.JSONObject jsonObject2 = new org.json.JSONObject(result2);

3. Continue to take the third layer of data, which is also a sub-object.
This object can be seen as an array object, there are many elements in JSON Array 2!!!

   String stat = jsonObject2.getString("stat");
                    String data = jsonObject2.getString("data");
                    org.json.JSONArray jsonArray2 = new org.json.JSONArray(data);

4. Layer 4. Each element in the array is also a jsonobject object object. Take out
org.json.JSONObject myjObject = jsonArray2.getJSONObject(i);

  for (int i = 0; i < jsonArray2.length(); i++) {
                        //Get each JsonObject object
                        org.json.JSONObject myjObject = jsonArray2.getJSONObject(i);
                        if (myjObject != null) {
                            Data data1 = new Data(myjObject);
                            Log.d("lihui", "Fragment onResponse getUniquekey---" + data1.getUniquekey());
                            Log.d("lihui", "Fragment onResponse data---" + data1);
                            list.add(data1);
                        }
                    }

Well, learn the json format analysis, there should be nothing that can be difficult for us, layer by layer analysis, here the main method is as follows:

org.json.JSONObject : This class, sdk Bring along
jsonObject.getString("reason");
org.json.JSONArray jsonArray2 = new org.json.JSONArray(data);
//Cycle:
 for (int i = 0; i < jsonArray2.length(); i++) {
                        //Get each JsonObject object
                        org.json.JSONObject myjObject = jsonArray2.getJSONObject(i);
                        if (myjObject != null) {
                            Data data1 = new Data(myjObject);
                            Log.d("lihui", "Fragment onResponse getUniquekey---" + data1.getUniquekey());
                            Log.d("lihui", "Fragment onResponse data---" + data1);
                            list.add(data1);
                        }
                    }



Finally, when we get the jsonobject object object, we can customize the object and construct the initialization.

  public Data(JSONObject jsonObject) throws  Exception{
        this.title=jsonObject.getString("title");
        this.date=jsonObject.getString("date");
        this.author_name=jsonObject.getString("author_name");
        this.thumbnail_pic_s=jsonObject.getString("thumbnail_pic_s");
        //this.thumbnail_pic_s02=jsonObject.getString("thumbnail_pic_s02");
        //this.thumbnail_pic_s03=jsonObject.getString("thumbnail_pic_s03");
        this.url=jsonObject.getString("url");
   //     this.uniquekey=jsonObject.getString("uniquekey");

    }

End~~~~~~~~~~~·

Topics: JSON Mobile Retrofit Fragment