2016-03-31 15 views
-5

i "Kategori" almak istiyorum dizi tepkisini nasıl ele Json verilerini ayrıştırmak ve kategorisine göre "Başlık" & "Fiyatı" zaten Volley JsonArrayRequest burada

JsonSchema2Pojo

gelen pojo sınıfları oluşturulmuş almak istiyor için gibi Volley & GsonRequest sınıfını kullanarak im taşıma cevabı:

 JsonArrayRequest req = new JsonArrayRequest(URL, new Response.Listener<JSONArray>() { 
      @Override 
        public void onResponse(JSONArray response) { 
        Log.d("response....",""+response); 
       if (response != null) { 
        showProgress(false); 
        Gson gson = new GsonBuilder().create(); 
        try { 
         Log.d("response....", "G00D"); 

        Item items = gson.fromJson(response,Item.class); 

        JSONArray json = new JSONArray(response); 
        Log.d("arrayJSon",""+json.get(0)); 

//What is need to do here tell me solution please......................... 
//.................dosomething here....................... 

        } 

Benim json tepki geçerli:

[{ 
        "category": "Espresso & Coffee", 
        "items": [{ 
        "title": "Vacuum Coffee", 
        "size": [{ 
         "label": "medium", 
         "price": 125 
        }, { 
        "label": "large", 
        "price": 145 
       }] 
      }, { 
       "title": "Cafe Latte", 
       "size": [{ 
        "label": "small", 
        "price": 115 
       }, { 
        "label": "medium", 
        "price": 135 
       }, { 
        "label": "large", 
        "price": 150 
       }] 
      }, { 
       "title": "Cafe Mocha", 
       "size": [{ 
        "label": "small", 
        "price": 115 
       }, { 
        "label": "medium", 
        "price": 135 
       }, { 
        "label": "large", 
        "price": 150 
       }] 
      }, { 
       "title": "White Mocha", 
       "size": [{ 
        "label": "small", 
        "price": 120 
       }, { 
        "label": "medium", 
        "price": 145 
       }, { 
        "label": "large", 
        "price": 155 
       }] 
      }, { 
       "title": "Caramel Macchiato", 
       "size": [{ 
        "label": "small", 
        "price": 125 
       }, { 
        "label": "medium", 
        "price": 145 
       }, { 
        "label": "large", 
        "price": 165 
       }] 
      }, { 
       "title": "Coffee De Leche", 
       "size": [{ 
        "label": "small", 
        "price": 130 
       }, { 
        "label": "medium", 
        "price": 145 
       }, { 
        "label": "large", 
        "price": 160 
       }] 
      }, { 
       "title": "Cafe-UK Coffee Lava", 
       "size": null, 
       "price": 145 
      }] 
     }, { 
      "category": "Frappe", 
      "items": [{ 
       "title": "Coffee Base", 
       "size": [{ 
        "label": "small", 
        "price": 140 
       }, { 
        "label": "medium", 
        "price": 155 
       }, { 
        "label": "large", 
        "price": 170 
       }] 
      }, { 
       "title": "Milk Base", 
       "size": [{ 
        "label": "small", 
        "price": 130 
       }, { 
        "label": "medium", 
        "price": 145 
       }, { 
        "label": "large", 
        "price": 160 
       }] 
      }, { 
       "title": "Cream Soda Float w\/Ice-cream", 
       "size": null, 
       "price": 140 
      }] 
     }, { 
      "category": "Milk Tea & Juice", 
      "items": [{ 
       "title": "Milk Tea", 
       "size": [{ 
        "label": "small", 
        "price": 105 
       }, { 
        "label": "medium", 
        "price": 120 
       }] 
      }, { 
       "title": "Hot Tea", 
       "size": null, 
       "price": 120 
      }, { 
       "title": "Italian Soda", 
       "size": [{ 
        "label": "small", 
        "price": 105 
       }, { 
        "label": "medium", 
        "price": 120 
       }, { 
        "label": "large", 
        "price": 140 
       }] 
      }, { 
       "title": "Juice", 
       "size": null, 
       "price": 105 
      }] 
      }, { 
      "category": "Food", 
      "items": [{ 
       "title": "Ultimate Chili Con Fries", 
       "size": null, 
       "price": 290 
      }, { 
       "title": "Nachos", 
       "size": null, 
       "price": 290 
      }, { 
       "title": "Marble Potato Fondue", 
       "size": null, 
       "price": 120 
      }, { 
       "title":"Breakfast", 
       "size":null, 
       "price":220 
      }]}] 
+0

Ayrıştırmaya çalıştığınız alanlar nelerdir? – malli

+0

"Kategori" almak istiyorum ve Kategorisine göre "Başlıklar" ve "Fiyat" kategorisine göre Üç Alanı Göstermek için RecyclerView'ı kullanıyorum. – Dinesh

cevap

3

Bir süre önce yaptığınız gibi sıkışıp kaldım ve json yerine json'u elle işledim. JsonArrayRequest kullanmak yerine JsonObjectRequest'i deneyin.

sonra böyle döner yukarı 'doğrama' her dizi olsun:

JsonArray c = response.getJSONArray("array"); 

Sonra i<c.length().

Ancak limitli döngü için geçerek dizinin içinde depolanan değerleri elde Eğer bu hat boyunca bir şey yapıyor olurdum dizisi ile bir arrray beri, burayı

for(int i=0; i<c.length(); i++){ 
//populates the array, in your case, jsonarray size = 4 
JSONObject jsonObject = c.getJSONObject(i); 
String cat= jsonObject.getString("category"); //gets category String 
JSONArray items = jsonObject.getJSONArray("items"); 
.... 

almak amacıyla, bir iç döngü yazma Nesnenizdeki dizi öğeleri .... umarım sizi kafa karıştırmak yerine sorunu çözmenize yardımcı olurum, lol. Örnek vermek gerekirse, burada hangisinin nereye gideceğini belirleyen bir tane var: https://github.com/codepath/android_guides/wiki/Rotten-Tomatoes-Networking-Tutorial

+0

teşekkürler @Sarbinar u r right ... Boyut Dizisi için Bu Öğeleri Diziyi kullanacağım ve JSONObject jsonObject1 = items.getJSONObject (j); Dize başlığı = jsonObject1.getString ("title"); JSONArray boyutları = jsonObject1.getJSONArray ("Boyut"); – Dinesh

+0

yup! veri yapınız basit değilse, verilerinizi ele almak için diğer kütüphanelere güvenmekten çok daha basittir. çoğu zaman gson –

+0

ile çalışmak için bir düpedüz bir acı olduğunu bana geri dönüşümlü olarak herhangi bir yaklaşım – Dinesh