如何从嵌套数组中获取数据?

时间:2018-01-21 07:09:02

标签: android json jsonparser

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

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

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

1 个答案:

答案 0 :(得分:1)

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

首先,您必须查询&#34;数据&#34;。你得到JSON-Array。所以你用指数遍历它。

因此,您在&#34;数据&#34;中查询特定索引。 (当然,你可以使用循环)。

对于每次迭代,您都会获得一个JSONObject,并在其中查询&#34; comments_data&#34;。

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