-1

I am trying to loop through an Array of JSON object, but my loop fetches and duplicates only the last element on the JSON Array. Please can someone help...

This is the JSON file am fetching the JSON Array from

{  "data":[  
  {  
     "sno":1,
     "id":"5",
     "title":"Position Yourself For Marriage2",
     "desc":"Position Yourself For Marriage",
     "free":"Yes",
     "image":"http:\/\/app-web.moneyacademy.ng\/uploads\/5a815bc805fc9cb5a8a619dc43ead169.jpg",
     "date_added":"2017-10-17 05:48PM",
     "no_comment":0,
     "comments":0
  },
  {  
     "sno":2,
     "id":"4",
     "title":"Position Yourself For Marriage",
     "desc":"Position Yourself For Marriage",
     "free":"Yes",
     "image":"http:\/\/app-web.moneyacademy.ng\/uploads\/4209d035814c5ae5d1978ad1d85fc65b.jpg",
     "date_added":"2017-10-17 05:47PM",
     "no_comment":0,
     "comments":0
  },
  {  
     "sno":3,
     "id":"3",
     "title":"This Is Great Again",
     "desc":"The details of how a UUID is generated are determined by the device manufacturer and are specific to the device's platform or model.The details of...",
     "free":"Yes",
     "image":"http:\/\/app-web.moneyacademy.ng\/uploads\/145277f3d0499ee8e0dafbac384ca9b4.jpg",
     "date_added":"2017-10-12 10:26PM",
     "no_comment":3,
     "comments":[  
        {  
           "id_comment":"5",
           "id_user":"1",
           "id_nugget":"3",
           "comment":"Thank you all for your comment. We love you",
           "status":"1",
           "date_commented":"2017-10-16 00:25:18"
        },
        {  
           "id_comment":"1",
           "id_user":"1",
           "id_nugget":"3",
           "comment":"This is great and I love money academy",
           "status":"1",
           "date_commented":"2017-10-14 00:00:00"
        },
        {  
           "id_comment":"2",
           "id_user":"2",
           "id_nugget":"3",
           "comment":"This is a great work and I love it",
           "status":"1",
           "date_commented":"2017-10-14 00:00:00"
         }
       ]
     }
   ]
 }

And this is my code where I am fetching and looping through the JSON Array

    ArrayList<nauget> naugets = new ArrayList<>();

    try {

        JSONObject baseJsonResponse = new JSONObject(freeNaugetJson);
        JSONArray dataArray = baseJsonResponse.getJSONArray("data");

        // If there are results in the data array
        for (int i = 0; i <= dataArray.length(); i++){

            String title = dataArray.getJSONObject(i).getString("title");
            String body = dataArray.getJSONObject(i).getString("desc");
            String totalComments = dataArray.getJSONObject(i).getString("no_comment");
            String image = dataArray.getJSONObject(i).getString("image");

            ArrayList<Comment> comments = new ArrayList<>();

            //fetch each comment detail
            if (Integer.parseInt(totalComments) > 0) {
                JSONArray commentArray = dataArray.getJSONObject(i).getJSONArray("comments");

                for (int j = 0; j < commentArray.length(); j++) {
                    String userID = commentArray.getJSONObject(i).getString("id_user");
                    String userComment = commentArray.getJSONObject(i).getString("comment");

                    comments.add(new Comment(userID, userComment));
                }
            }

            Log.d("debugger", "Looped comments" + comments);
            // Create a new nauget object
            naugets.add(new nauget(title, body, image, totalComments, comments));
        }
    } catch (JSONException e) {
        Log.e(LOG_TAG, "Problem parsing the nauget JSON results", e);
    }

This is what I get as my result

D/debugger: Looped comments[Person [name=2, age=This is a great work and I love it], Person [name=2, age=This is a great work and I love it], Person [name=2, age=This is a great work and I love it]]
ADM
  • 18,477
  • 11
  • 47
  • 78
Peter Akwa
  • 61
  • 1
  • 5
  • "i <= dataArray.length()" ?. This will throw `IndexOutofBoundException`. – ADM Feb 08 '18 at 04:56
  • for (int j = 0; j < commentArray.length(); j++) { String userID = commentArray.getJSONObject(i).getString("id_user"); String userComment = commentArray.getJSONObject(i).getString("comment"); comments.add(new Comment(userID, userComment)); } You have made mistake in this section. Inside this for loop you have to use variable j instead of variable i. – Megha Maniar Feb 08 '18 at 04:59
  • @Peter please check my below answer. – Ratilal Chopda Feb 08 '18 at 06:02

2 Answers2

1

change your condition as below:

for (int i = 0; i < dataArray.length(); i++)

and update your inner for loop too. Use j instead of I.

               for (int j = 0; j < commentArray.length(); j++) {
                    String userID = commentArray.getJSONObject(j).getString("id_user");
                    String userComment = commentArray.getJSONObject(j).getString("comment");

                    comments.add(new Comment(userID, userComment));
                }
Zaid Mirza
  • 3,182
  • 1
  • 20
  • 39
0

Try this Change your condition

Use This

for(int i = 0; i < dataArray.length(); i++)

Instead of this

for(int i = 0; i <= dataArray.length(); i++)

Complete ANSWER

for (int i = 0; i <dataArray.length(); i++)
{

        String title = dataArray.getJSONObject(i).getString("title");
        String body = dataArray.getJSONObject(i).getString("desc");
        String totalComments = dataArray.getJSONObject(i).getString("no_comment");
        String image = dataArray.getJSONObject(i).getString("image");

        ArrayList<Comment> comments = new ArrayList<>();

        //fetch each comment detail
        if (Integer.parseInt(totalComments) > 0) 
        {
           JSONArray commentArray = dataArray.getJSONObject(i).getJSONArray("comments");

           for (int j = 0; j < commentArray.length(); j++) {

              JSONObject cmtObject = commentArray.getJSONObject(j);
              String userID = cmtObject.getString("id_user");
              String userComment = cmtObject.getString("comment");

              Log.i("ID", ":" + userID);
              Log.i("Comment", ":" + userComment);

              comments.add(new Comment(userID, userComment));
            }
        }

        Log.d("debugger", "Looped comments" + comments);
        // Create a new nauget object
        naugets.add(new nauget(title, body, image, totalComments, comments));
}

OUTPUT

I/ID: :1
I/Comment: :Thank you all for your comment. We love you
I/ID: :1
I/Comment: :This is great and I love money academy
I/ID: :2
I/Comment: :This is a great work and I love it
Ratilal Chopda
  • 4,113
  • 4
  • 17
  • 29