-2

我必须从嵌套在另一个数组中的数组中获取值。我可以从第一个数组中获取值,但无法从第二个数组中获取值。

我能够从主数组中读取值,但无法从嵌套在其中的数组中读取值。

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);
  }
}

这是我的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"
                }
            ]
        }
    ]
}
4

1 回答 1

1

仔细注意 JSON 是如何构建的。

首先,您必须查询“数据”。你得到 JSON 数组。所以你通过索引遍历它。

因此,您查询“数据”中的特定索引(当然,您可以使用循环)。

对于每次迭代,您都会获得一个 JSONObject,并在其中查询“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");
于 2018-01-21T08:02:34.423 回答