将 JSONObject 中的 JSONArray 转换为单独的 JSONObject

Converting JSONArray within JSONObject into individual JSONObjects

我有一个 JSON 响应,看起来像这样(响应更大,但我已将其截断以用于此示例):

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

如您所见,顶部有 2 个名称 'foods' 和 'food' - 如果只有一个名称,我知道如何获取 JSON 对象,但是是两个,这让我很困惑 - 我该怎么办?

我有这个:

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

但我得到以下信息:

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)

请试试这个,你正在错误地解析你的JSON

您的回复中有 食物 JSONObject食物 JSONArray

真正的解析方式应该是这样的

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

这一行得到整个 JSONObject 包括 "foods"

JSONObject jsonObj = new JSONObject(response);

你划掉了 "foods" 所以它不起作用,像这样改变它:

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