我有一个看起来像这样的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)"
}
]
}
}
正如你所看到的,顶部'食物'和'食物'有两个名字 - 我知道如果只有一个名字,如何获得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)
答案 0 :(得分:4)
请试试这个,你正在错误地解析你的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;
}
}
答案 1 :(得分:0)
此行获取整个JSONObject
包括&#34;食物&#34;
JSONObject jsonObj = new JSONObject(response);
你越过了&#34;食物&#34;所以它不起作用,改变它:
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");
....