2016-04-07 20 views
0

ich eine JSON-Antwort haben, die wie folgt aussieht (die Antwort ist größer, aber ich habe es für dieses Beispiel abgeschnitten):Converting JSONArray innerhalb JSONObject in einzelne JSONObjects

{ 
    "foods":{ 
     "food":[ 
     { 
      "brand_name":"McDonald's", 
      "food_description":"Per 1 burger - Calories: 530kcal | Fat: 27.00g | Carbs: 47.00g | Protein: 24.00g", 
      "food_id":"3145844", 
      "food_name":"Big Mac", 
      "food_type":"Brand", 
      "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/mcdonalds\/big-mac" 
     }, 
     { 
      "brand_name":"McDonald's", 
      "food_description":"Per 1 big mac - Calories: 330kcal | Fat: 26.00g | Carbs: 6.00g | Protein: 18.00g", 
      "food_id":"3137812", 
      "food_name":"Big Mac (No Bun)", 
      "food_type":"Brand", 
      "food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/mcdonalds\/big-mac-(no-bun)" 
     } 
     ] 
    } 
} 

Wie Sie sehen können, gibt es zwei Namen an der Spitze "Essen" und "Essen" - ich weiß, wie man die JSON-Objekte bekommt, wenn es nur einen Namen gibt, aber es gibt zwei, und es ist verwirrend die Hölle von mir - was mache ich?

Ich habe dies:

public ArrayList<Item> getOnlineFoods(String response) { 

     ArrayList<Item> itemList = new ArrayList<>(); 
     try { 
      JSONObject jsonObj = new JSONObject(response); 
      Log.w("USER: ", "food:" + jsonObj.toString()); 
      JSONArray resultsArray = jsonObj.getJSONArray("foods"); 
      for (int i = 0; i < resultsArray.length(); i++) { 
       JSONObject row = resultsArray.getJSONObject(i); 

       int id = row.getInt("food_id"); 
       String name = row.getString("food_name"); 
       String desc = row.getString("food_description"); 
       String cat = row.getString("food_type"); 
       String imgURL = row.getString("food_url"); 
       FoodListItem fli = new FoodListItem(this, 
         id, name, 
         "100", "g", 
         cat, "Cal", imgURL); 
       itemList.add(fli); 
      } 

      return itemList; 

     } catch (JSONException e) { 
      e.printStackTrace(); 
      return itemList; 
     } 
    } 

Aber ich bin immer folgendes:

04-07 16:24:08.455 17495-18174/com.example.chris.rapical W/System.err: org.json.JSONException: Value {"food":[{"brand_name":"McDonald's","food_description":"Per 1 burger - Calories: 530kcal | Fat: 27.00g | Carbs: 47.00g | Protein: 24.00g","food_id":"3145844","food_name":"Big Mac","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/mcdonalds\/big-mac"},{"brand_name":"McDonald's","food_description":"Per 1 big mac - Calories: 330kcal | Fat: 26.00g | Carbs: 6.00g | Protein: 18.00g","food_id":"3137812","food_name":"Big Mac (No Bun)","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/mcdonalds\/big-mac-(no-bun)"},{"brand_name":"McDonald's","food_description":"Per 1 sandwich - Calories: 390kcal | Fat: 16.00g | Carbs: 40.00g | Protein: 21.00g","food_id":"1136746","food_name":"Big Mac (Plain)","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/mcdonalds\/big-mac-(plain)"},{"brand_name":"Big Boy","food_description":"Per 1 sandwich - Calories: 774kcal | Fat: 26.00g | Carbs: 95.00g | Protein: 40.00g","food_id":"2282394","food_name":"Big Boy Sandwich","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/big-boy\/big-boy-sandwich"},{"brand_name":"Big Red","food_description":"Per 1 can - Calories: 195kcal | Fat: 0.00g | Carbs: 48.00g | Protein: 0.00g","food_id":"2162423","food_name":"Big Blue","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/big-red\/big-blue"},{"brand_name":"Big Red","food_description":"Per 1 can - Calories: 0kcal | Fat: 0.00g | Carbs: 0.00g | Protein: 0.00g","food_id":"131276","food_name":"Diet Big Soda (Can)","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/big-red\/diet-big-soda-(can)"},{"food_description":"Per 218g - Calories: 559kcal | Fat: 30.82g | Carbs: 42.43g | Protein: 26.84g","food_id":"2723","food_name":"Double Cheeseburger with Mayonnaise or Salad Dressing on Double-Decker Bun","food_type":"Generic","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/generic\/double-cheeseburger-(2-patties)-with-mayonnaise-or-salad-dressing-on-double-decker-bun"},{"brand_name":"Pillsbury","food_description":"Per 1 cookie - Calories: 170kcal | Fat: 9.00g | Carbs: 22.00g | Protein: 1.00g","food_id":"198561","food_name":"Cookies Ready To Bake - Big Deluxe White Chunk Macadamia Nut","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/pillsbury\/cookies-ready-to-bake---big-deluxe-white-chunk-macadamia-nut"},{"brand_name":"Frisch's Big Boy","food_description":"Per 1 sandwich - Calories: 680kcal | Fat: 44.00g | Carbs: 32.00g | Protein: 34.00g","food_id":"735121","food_name":"Big Boy","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/frischs-big-boy\/big-boy"},{"brand_name":"Big Red","food_description":"Per 8 fl oz - Calories: 0kcal | Fat: 0.00g | Carbs: 0.00g | Protein: 0.00g","food_id":"326142","food_name":"Diet Big Red Soda","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/big-red\/diet-big-red-soda"},{"brand_name":"Pillsbury","food_description":"Per 2 cookies - Calories: 180kcal | Fat: 10.00g | Carbs: 21.00g | Protein: 2.00g","food_id":"228964","food_name":"Big Deluxe White Chunk Macadamia Nut Cookie Dough","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/pillsbury\/big-deluxe-white-chunk-macadamia-nut-cookie-dough"},{"brand_name":"Big Red","food_description":"Per 1 can - Calories: 0kcal | Fat: 0.00g | Carbs: 0.00g | Protein: 0.00g","food_id":"3661946","food_name":"Big Red Zero (Can)","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/big-red\/big-red-zero-(can)"},{"brand_name":"Shane's Rib Shack","food_description":"Per 1 sandwich - Calories: 660kcal | Fat: 18.00g | Carbs: 34.00g | Protein: 29.00g","food_id":"3273888","food_name":"Big Dad Pulled Pork Sandwich","food_type":"Brand","food_url":"http:\/\/www.fatsecret.com\/calories-nutrition\/shanes-rib-shack\/big-dad-pulled-pork-sandwich"},{"brand_name":"Big Boy","food_description":"Per 1 
04-07 16:24:08.455 17495-18174/com.example.chris.rapical W/System.err:  at org.json.JSON.typeMismatch(JSON.java:100) 
04-07 16:24:08.455 17495-18174/com.example.chris.rapical W/System.err:  at org.json.JSONObject.getJSONArray(JSONObject.java:588) 
+0

Lebensmittel ist kein Array ist ein Objekt, das Array Sie holen sollte, ist „Nahrung“ –

+0

@NaN hat es versucht - funktioniert nicht – blueprintChris

+0

Haben Sie versucht, mit Gson? – HendraWD

Antwort

4

Bitte versuchen Sie dies, Sie sind Parsen Ihre JSON falsch

Es gibt LebensmittelJSONObject und EssenJSONArray in Ihrer Antwort

So wahr Art und Weise zu analysieren, es wie dieses

public ArrayList<Item> getOnlineFoods(String response) { 

    ArrayList<Item> itemList = new ArrayList<>(); 
    try { 
     JSONObject jsonObj = new JSONObject(response); 
     Log.w("USER: ", "food:" + jsonObj.toString()); 
     JSONObject tempObjs = jsonObj.optJSONObject("foods"); 
     JSONArray resultsArray = tempObjs .getJSONArray("food"); 
     for (int i = 0; i < resultsArray.length(); i++) { 
      JSONObject row = resultsArray.getJSONObject(i); 

      int id = row.getInt("food_id"); 
      String name = row.getString("food_name"); 
      String desc = row.getString("food_description"); 
      String cat = row.getString("food_type"); 
      String imgURL = row.getString("food_url"); 
      FoodListItem fli = new FoodListItem(this, 
        id, name, 
        "100", "g", 
        cat, "Cal", imgURL); 
      itemList.add(fli); 
     } 

     return itemList; 

    } catch (JSONException e) { 
     e.printStackTrace(); 
     return itemList; 
    } 
} 
+0

Sie wunderbarer Mensch zu erhalten - danke! – blueprintChris

+0

Ich bin glücklich, wenn es funktioniert :) Bitte akzeptieren Sie die Antwort, wenn es funktioniert –

+0

Ich werde in 7 Minuten akzeptieren, wenn es mir erlaubt – blueprintChris

0

Diese Zeile wird die gesamte JSONObject einschließlich „Lebensmittel“

JSONObject jsonObj = new JSONObject(response); 

Sie abgehakt die „Lebensmittel“ sein wird, damit es nicht Arbeit, ändern Sie es wie folgt aus:

public ArrayList<Item> getOnlineFoods(String response) { 

    ArrayList<Item> itemList = new ArrayList<>(); 
    try { 
     JSONObject jsonObj = new JSONObject(response); 
     //foods is an json object 
     JSONObject foodObj = jsonObj.getJSONObject("foods"); 
     //food is an json array 
     JSONArray resultsArray = jsonObj.getJSONArray("food"); 
     .... 
Verwandte Themen