I'm having some problems building an ArrayList from a JSONArray I called from an API Get. What am I doing wrong in my call?
The JSONArray i'm trying to receive is "recipes". I only need the "title" and "image_url".
{"count": 2, "recipes": [{"publisher": "The Pioneer Woman", "f2f_url": "http://food2fork.com/view/47024", "title": "Perfect Iced Coffee", "source_url": "http://thepioneerwoman.com/cooking/2011/06/perfect-iced-coffee/", "recipe_id": "47024", "image_url": "http://static.food2fork.com/icedcoffee5766.jpg", "social_rank": 100.0, "publisher_url": "http://thepioneerwoman.com"}, {"publisher": "Closet Cooking", "f2f_url": "http://food2fork.com/view/35382", "title": "Jalapeno Popper Grilled Cheese Sandwich", "source_url": "http://www.closetcooking.com/2011/04/jalapeno-popper-grilled-cheese-sandwich.html", "recipe_id": "35382", "image_url": "http://static.food2fork.com/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B50 0fd186186.jpg", "social_rank": 100.0, "publisher_url": "http://closetcooking.com"}]}
Method I use to retrieve the JSONArray and store it into an ArrayList.
public List getRecipes(String jsonText) {

List list = new ArrayList();

try {
JSONObject jsonObject = new JSONObject(jsonText);
JSONArray jsonArrayRecipes = jsonObject.getJSONArray("recipes");

for (int i = 0; i < jsonArrayRecipes.length(); i++) {

JSONObject jsonObjectRecipe = jsonArrayRecipes.getJSONObject(i);

Recipe recipe = new Recipe();
recipe.setImage_url(jsonObjectRecipe.getString("ti tle"));
recipe.setName(jsonObjectRecipe.getString("image_u rl"));
list.add(recipe);
}
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}

return list;
}
Any help would be appreciated. Thanks in advance!


Check Solution