Search code examples
javajsonarraylistcodenameoneorg.json

Parse JSONArray present in generic ArrayList


I have some issue with JSONArray, As I am having a JSON data present in generic ArrayList but I don't have any idea that how to parse that json data and display in list, I am using org.json library

Below is my json data which is present in array list:

[{"story":"Gaurav Takte shared a link.","created_time":"2017-02-14T19:08:34+0000","id":"1323317604429735_1307213186040177"},{"story":"Gaurav Takte shared a link.","created_time":"2017-02-02T14:22:50+0000","id":"1323317604429735_1295671703860992"},{"message":"Hurray....... INDIA WON KABBADI WORLD CUP 2016","created_time":"2016-10-22T15:55:04+0000","id":"1323317604429735_1182204335207730"},{"story":"Gaurav Takte updated his profile picture.","created_time":"2016-10-21T05:35:21+0000","id":"1323317604429735_1180682575359906"},{"message":"Friends like all of you \u2026 I would love to keep forever.\n#oldmemories with # besties \n#happydays","story":"Gaurav Takte with Avi Bhalerao and 5 others.","created_time":"2016-10-21T05:33:55+0000","id":"1323317604429735_1180682248693272"},{"message":"\"सर्वांना गणेशचतुर्थीच्या हार्दीक शुभेच्छा.\nतुमच्या मनातील सर्व मनोकामना पूर्ण होवोत , सर्वांना\nसुख, समृध्दी, ऎश्वर्य,शांती,आरोग्य लाभो हीच\nबाप्पाच्या चरणी प्रार्थना. \"\nगणपती बाप्पा मोरया , मंगलमुर्ती मोरया !!!","story":"Gaurav Takte with Avi Bhalerao and 18 others.","created_time":"2016-09-05T05:06:58+0000","id":"1323317604429735_1133207030107461"}]

And here is my code:

ArrayList data_arr1= (ArrayList) ((Map) parsed.get("posts")).get("data"); JSONArray array = new JSONArray(data_arr1); for(int i = 0; i < array.length(); i++){ try { JSONObject obj = array.getJSONObject(i); Log.p(obj.toString()); } catch (JSONException ex) { ex.printStackTrace(); } }

So how can i parse this json using org.json library.


Solution

  • Here is the best solution of in-proper json response. You can try this code I hope it works good..

                String result = "Your JsonArray Data Like [{}]";
    
                ArrayList<String> arrayList = new ArrayList<>();
                try {
                    JSONArray jsonarray = new JSONArray(result);
                    for (int i = 0; i < jsonarray.length(); i++) {
                        JSONObject jsonobject = jsonarray.getJSONObject(i);
                        String story = null;
                        try {
                            story = jsonobject.getString("story");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        String msg = null;
                        try {
                            msg = jsonobject.getString("message");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        String ct = jsonobject.getString("created_time");
                        String id = jsonobject.getString("id");
    
                        if (msg == null){
                            msg = "";
                        }
                        if (story == null){
                            story = "";
                        }
                        arrayList.add(story + msg + ct + id);
          //            Smodel is getter model
          //            arrayList.add(new Smodel(story, msg, ct, id));
                    }
    
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }