0

I have a JSON response that looks like this (the response is larger but I have truncated it for this example):

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

As you can see, there are 2 names at the top 'foods' and 'food' - I know how to get the JSON objects if there is only one name, but there are two and it's confusing the hell out of me - what do I do?

I have this:

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

But I am getting the following:

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)
7
  • foods is not an array, is an object, the array you should fetch is "food" Commented Apr 7, 2016 at 15:32
  • @NaN tried it - doesn't work Commented Apr 7, 2016 at 15:33
  • Have you tried using Gson? Commented Apr 7, 2016 at 15:33
  • because you didn't fetch foods object! the jsonObj actually is the whole object including foods, got it? Commented Apr 7, 2016 at 15:34
  • @NaN how about proving me with an answer then? Commented Apr 7, 2016 at 15:34

2 Answers 2

4

Please try this, you are parsing your JSON wrongly

There are foods JSONObject and food JSONArray in your response

So true way to parse it will be like this

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm happy if it is working :) please accept answer if it works
0

This line gets the whole JSONObject including "foods"

JSONObject jsonObj = new JSONObject(response);

You crossed off the "foods" so it doesn't work, Change it like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.