2017-07-28 45 views
-3

幫助球員,我試圖解析這個嵌套的JSON對象。當我檢查logcat時,它不會迭代到麪食的下一個JSONObject。這隻能說明Android Volley並沒有遍歷整個JSON對象

「名」 第一的JSONObject: 「奶油培根蘑菇」, 「細節」: 「」, 「價格」: 「」, 「形象」:{ 「URL」: 「」}

之後,它無限循環一次又一次。

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

    } 

} 

]

這是我的代碼:

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

}

這是我的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:詳情:Creamyyy 07-28 14:54:53.692 6222-6222 /? D/SAD:名稱:Creamy Bacon Mushroom 07-28 14:54:53.692 6222-6222 /? D/SAD:價格:170 07-28 14:54:53.692 6222-6222 /? d/SAD:細節:Creamyyy

+2

問題在** i ++ ** – Enzokie

回答

1

變化我+ +在內部J ++ for循環

+0

謝謝阿尼爾:)我沒有注意到 – StevenF

0

使用GSON格式解析JSON。你可以看到我的回答hear。我希望它能幫助你。