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]]