2

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.

4
  • Have you tried using a search engine? I get the feeling that there are 10 question like this each day here ... Commented Mar 23, 2017 at 6:22
  • And hint: read about raw generic types. You are using them; but you should better not. Commented Mar 23, 2017 at 6:23
  • @GRV_Droid Do not post code in a comment. Edit the question to clarify. Delete the comment. Commented Mar 23, 2017 at 6:37
  • I tested your json response example it's work fine.. See my Answer. Commented Mar 23, 2017 at 7:01

1 Answer 1

2

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();
            }
Sign up to request clarification or add additional context in comments.

3 Comments

can you explain what result field contain? means json response or my arraylist which contain json
result is String value of response Jsonarray
Welcome. accept my answer and close this que. @GRV_Droid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.