-2

I have to fetch the value from an array nested inside another array. I can fetch values from the first array but not able to fetch values from the second array.

I am able to read the values from the main array but I am unable to read the values from the array which is nested inside it.

JSONObject jsono = new JSONObject(res);
JSONArray jarray = jsono.getJSONArray("data");

for (int i = 0; i < jarray.length(); i++)
{
  JSONObject object = jarray.getJSONObject(i);

  JSONArray jarray1 = object.getJSONArray("comments_data");

  for (int j = 0; j < jarray1.length(); j++)
  {
    JSONObject object1 = jarray1.getJSONObject(j);

    String Namee = object1.getString("username");
    String Ratingg = object1.getString("rating");
    String Commentt = object1.getString("comment");

    Comments comments1 = new Comments();

    comments1.setUsername(Namee);
    comments1.setRating(Ratingg);
    comments1.setComments(Commentt);
    comments1.setProfileimage(R.drawable.fav);

    commentsList.add(comments1);
  }
}

And this is my json.

{
    "status": "success",
    "msg": " Menu Details",
    "data": [
        {
            "id": "1",
            "rest_id": "1",
            "menu_rate": "100",
            "collection_time": "2:22pm",
            "quantity_left": "3",
            "food_type": "veg",
            "img1": "",
            "img2": "",
            "img3": "",
            "date": "",
            "menu_name": "",
            "comments_data": [
                {
                    "id": "20",
                    "user_id": "127",
                    "res_id": "1",
                    "comment": "shreyansh s",
                    "date": "0000-00-00 00:00:00",
                    "rating": "0.0",
                    "username": "lucky",
                    "userimage": "123"
                },
                {
                    "id": "19",
                    "user_id": "126",
                    "res_id": "1",
                    "comment": "das",
                    "date": "0000-00-00 00:00:00",
                    "rating": "3.0",
                    "username": "shrey srivastava",
                    "userimage": "123"
                },
                {
                    "id": "18",
                    "user_id": "126",
                    "res_id": "1",
                    "comment": "",
                    "date": "0000-00-00 00:00:00",
                    "rating": "3.0",
                    "username": "shrey srivastava",
                    "userimage": "123"
                },
                {
                    "id": "17",
                    "user_id": "126",
                    "res_id": "1",
                    "comment": "sakjbdkjasbk",
                    "date": "0000-00-00 00:00:00",
                    "rating": "3.0",
                    "username": "shrey srivastava",
                    "userimage": "123"
                },
                {
                    "id": "16",
                    "user_id": "107",
                    "res_id": "1",
                    "comment": "hello",
                    "date": "0000-00-00 00:00:00",
                    "rating": "5",
                    "username": "shreyansh",
                    "userimage": ""
                },
                {
                    "id": "15",
                    "user_id": "107",
                    "res_id": "1",
                    "comment": "hello",
                    "date": "0000-00-00 00:00:00",
                    "rating": "5",
                    "username": "shreyansh",
                    "userimage": "123"
                },
                {
                    "id": "14",
                    "user_id": "107",
                    "res_id": "1",
                    "comment": "hello",
                    "date": "0000-00-00 00:00:00",
                    "rating": "5",
                    "username": "shreyansh",
                    "userimage": "123"
                },
                {
                    "id": "6",
                    "user_id": "82",
                    "res_id": "1",
                    "comment": "good",
                    "date": "0000-00-00 00:00:00",
                    "rating": "",
                    "username": "jaim",
                    "userimage": "google.com"
                }
            ]
        }
    ]
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • what is "JSONObject"? which package are you using? – OhadR Jan 21 '18 at 07:25
  • Try this: https://stackoverflow.com/a/9606629/5167909 – Faysal Ahmed Jan 21 '18 at 07:29
  • you can consider working with more friendly packages, such as org.codehaus.jettison.json (jettison.jar) or json-simple.jar... LMK if that helps a bit – OhadR Jan 21 '18 at 07:30
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jan 21 '18 at 11:12

1 Answers1

1

Note carefully how you JSON is built.

First, you have to query for "data". You get JSON-Array. So you traverse it by the indices.

Hence, you query for the specific index in "data" (you can use a loop, of course).

For each iteration, you get a JSONObject, and in it you query for "comments_data".

String json = "{ \"status\": \"success\", \"msg\": \" Menu Details\", \"data\": [ { \"id\": \"1\", \"rest_id\": \"1\", \"menu_rate\": \"100\", \"collection_time\": \"2:22pm\", \"quantity_left\": \"3\", \"food_type\": \"veg\", ...

JSONObject object = new JSONObject(json);

JSONObject jarray1 = jarray_data.getJSONObject(0);
JSONArray comments = jarray1.getJSONArray("comments_data");
OhadR
  • 8,276
  • 3
  • 47
  • 53