-3

Help guys, I am trying to parse this nested JSON Objects. When I checked the logcat it does not iterate to the next JSONObject of pasta. It only shows the first JSONObject of

"name" : "creamy bacon mushroom", "details" : "", "price" : "", "image" : { "url" : "" }

after that it infinitely loops over and over again.

This is my JSON:

[ 
{ 
    "food" : {
        "pasta" : [
            {
                "name" : "Creamy Bacon Mushroom",
                "price" : 170,
                "details" : "Creamyyy",
                "image" : {
                    "url" : "/SAD/uploads/food/images/1/food_pasta_creamybaconmushroom.jpg"
                }
            },
            {
                "name" : "Vigan Longganisa Penne",
                "price" : 169,
                "details" : "Vigannnnn",
                "image" : {
                    "url" : "/SAD/uploads/food/images/2/food_pasta_viganlonganissapenne.jpg"
                }
            },
            {
                "name" : "Spanish Sardine Pesto",
                "price" : 168,
                "details" : "Spanishhhhh",
                "image" : {
                    "url" : "/SAD/uploads/food/images/3/food_pasta_spanishsardinepesto.jpg"
                }
            }
        ]

    }

}

]

This is my code:

   StringRequest stringRequest = new StringRequest(url, new com.android.volley.Response.Listener<String>() {
       @Override
       public void onResponse(String response) {
           try {
               JSONArray jsonArray = new JSONArray(response);
               for(int i=0;i<response.length();i++){
                   JSONObject jsonObject = jsonArray.getJSONObject(i);
                   JSONObject jsonObjectFood = jsonObject.getJSONObject("food");
                   JSONArray jsonArrayPasta = jsonObjectFood.getJSONArray("pasta");
                   Log.d(StringConfig.LOG_TAG, jsonArrayPasta.toString());

                   for(int j=0; j<jsonArrayPasta.length();i++){
                       JSONObject jsonObject1 = jsonArrayPasta.getJSONObject(j);
                       String jsonName = jsonObject1.getString("name");
                       String jsonPrice = jsonObject1.getString("price");
                       String jsonDetails = jsonObject1.getString("details");

                       JSONObject jsonObjectImage = jsonObject1.getJSONObject("image");
                       String jsonImage = jsonObjectImage.getString("url");


                       Log.d(StringConfig.LOG_TAG, "name : " + jsonName);
                       Log.d(StringConfig.LOG_TAG, "price : " + jsonPrice);
                       Log.d(StringConfig.LOG_TAG, "details : " + jsonDetails);
                       Log.d(StringConfig.LOG_TAG, "imageUrl : " + jsonImage);
                   }


               }

           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
   }, new com.android.volley.Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {

       }
   });
   requestQueue.add(stringRequest);

}

This is my logcat:

07-28 14:54:53.691 6222-6222/? D/SAD: name : Creamy Bacon Mushroom
07-28 14:54:53.691 6222-6222/? D/SAD: price : 170
07-28 14:54:53.691 6222-6222/? D/SAD: details : Creamyyy
07-28 14:54:53.691 6222-6222/? D/SAD: imageUrl : /SAD/uploads/food/images/1/food_pasta_creamybaconmushroom.jpg
07-28 14:54:53.691 6222-6222/? D/SAD: name : Creamy Bacon Mushroom
07-28 14:54:53.691 6222-6222/? D/SAD: price : 170
07-28 14:54:53.691 6222-6222/? D/SAD: details : Creamyyy
07-28 14:54:53.691 6222-6222/? D/SAD: imageUrl : /SAD/uploads/food/images/1/food_pasta_creamybaconmushroom.jpg
07-28 14:54:53.691 6222-6222/? D/SAD: name : Creamy Bacon Mushroom
07-28 14:54:53.691 6222-6222/? D/SAD: price : 170
07-28 14:54:53.691 6222-6222/? D/SAD: details : Creamyyy
07-28 14:54:53.691 6222-6222/? D/SAD: imageUrl : /SAD/uploads/food/images/1/food_pasta_creamybaconmushroom.jpg
07-28 14:54:53.691 6222-6222/? D/SAD: name : Creamy Bacon Mushroom
07-28 14:54:53.691 6222-6222/? D/SAD: price : 170
07-28 14:54:53.691 6222-6222/? D/SAD: details : Creamyyy
07-28 14:54:53.691 6222-6222/? D/SAD: imageUrl : /SAD/uploads/food/images/1/food_pasta_creamybaconmushroom.jpg
07-28 14:54:53.691 6222-6222/? D/SAD: name : Creamy Bacon Mushroom
07-28 14:54:53.691 6222-6222/? D/SAD: price : 170
07-28 14:54:53.691 6222-6222/? D/SAD: details : Creamyyy
07-28 14:54:53.691 6222-6222/? D/SAD: imageUrl : /SAD/uploads/food/images/1/food_pasta_creamybaconmushroom.jpg
07-28 14:54:53.691 6222-6222/? D/SAD: name : Creamy Bacon Mushroom
07-28 14:54:53.691 6222-6222/? D/SAD: price : 170
07-28 14:54:53.691 6222-6222/? D/SAD: details : Creamyyy
07-28 14:54:53.691 6222-6222/? D/SAD: imageUrl : 

/SAD/uploads/food/images/1/food_pasta_creamybaconmushroom.jpg 07-28 14:54:53.692 6222-6222/? D/SAD: details : Creamyyy 07-28 14:54:53.692 6222-6222/? D/SAD: name : Creamy Bacon Mushroom 07-28 14:54:53.692 6222-6222/? D/SAD: price : 170 07-28 14:54:53.692 6222-6222/? D/SAD: details : Creamyyy

forestvine
  • 537
  • 6
  • 12

2 Answers2

1

Change i++ to j++ in inner for loop

Anil
  • 1,087
  • 1
  • 11
  • 24
0

Use GSON format to parse JSON. you can see my answer hear. I hope it will help you.

Sanjay Bhalani
  • 2,424
  • 18
  • 44