如何将此JSONArray存储到ArrayList中?

时间:2015-12-10 08:53:38

标签: java android json arraylist

我在使用API​​ Get调用的ArrayList构建JSONArray时遇到了一些问题。我收到一个空列表。我的电话里有什么问题?

我试图接收的JSONArray是" recipes"。我只需要" title"和" 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/Jalapeno2BPopper2BGrilled2BCheese2BSandwich2B12B500fd186186.jpg", "social_rank": 100.0, "publisher_url": "http://closetcooking.com"}]}

我用来检索JSONArray并将其存储到ArrayList中的方法。

public class JsonHelper {
public List<Recipe> getRecipes(String jsonText) {

    List<Recipe> list = new ArrayList<Recipe>();

    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("title"));
            recipe.setName(jsonObjectRecipe.getString("image_url"));
            list.add(recipe);
        }
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return list;
}}

活动类中使用的方法。我在API调用中模糊了devkey(链接正在工作,请参见上面的结果)。

 private void readRecipes()
{
    HttpReader httpReader = new HttpReader();
    httpReader.setOnResultReadyListener(new HttpReader.OnResultReadyListener() {
        @Override
        public void resultReady(String result) {
            JsonHelper jsonHelper = new JsonHelper();
            recipes = jsonHelper.getRecipes(result);
        }
    });


    httpReader.execute("http://food2fork.com/api/search?key={devkey}&q=");
}

任何帮助将不胜感激。提前谢谢!

2 个答案:

答案 0 :(得分:0)

我认为它适合你

public ArrayList<HashMap<String, String>> getRecipes(String jsonText) {
        ArrayList<HashMap<String, String>> arra_list = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonText);
            JSONArray recipesJsonArray = jsonObject.getJSONArray("recipes");
            for (int i = 0; i < recipesJsonArray.length(); i++) {
                HashMap<String, String> hashMap = new HashMap<>();
                JSONObject jsonObject1 = recipesJsonArray.getJSONObject(i);
                hashMap.put("title", jsonObject1.getString("title"));
                hashMap.put("image_url", jsonObject1.getString("image_url"));
                arra_list.add(hashMap);
            }

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

答案 1 :(得分:0)

首先创建一个像这样的类名称食谱

public class Recipe {

private String publisher;
private String f2fUrl;
private String title;
private String sourceUrl;
private String recipeId;
private String imageUrl;
private Double socialRank;
private String publisherUrl;

/**
* 
* @return
* The publisher
*/
public String getPublisher() {
return publisher;
}

/**
* 
* @param publisher
* The publisher
*/
public void setPublisher(String publisher) {
this.publisher = publisher;
}

/**
* 
* @return
* The f2fUrl
*/
public String getF2fUrl() {
return f2fUrl;
}

/**
* 
* @param f2fUrl
* The f2f_url
*/
public void setF2fUrl(String f2fUrl) {
this.f2fUrl = f2fUrl;
}

/**
* 
* @return
* The title
*/
public String getTitle() {
return title;
}

/**
* 
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}

/**
* 
* @return
* The sourceUrl
*/
public String getSourceUrl() {
return sourceUrl;
}

/**
* 
* @param sourceUrl
* The source_url
*/
public void setSourceUrl(String sourceUrl) {
this.sourceUrl = sourceUrl;
}

/**
* 
* @return
* The recipeId
*/
public String getRecipeId() {
return recipeId;
}

/**
* 
* @param recipeId
* The recipe_id
*/
public void setRecipeId(String recipeId) {
this.recipeId = recipeId;
}

/**
* 
* @return
* The imageUrl
*/
public String getImageUrl() {
return imageUrl;
}

/**
* 
* @param imageUrl
* The image_url
*/
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}

/**
* 
* @return
* The socialRank
*/
public Double getSocialRank() {
return socialRank;
}

/**
* 
* @param socialRank
* The social_rank
*/
public void setSocialRank(Double socialRank) {
this.socialRank = socialRank;
}

/**
* 
* @return
* The publisherUrl
*/
public String getPublisherUrl() {
return publisherUrl;
}

/**
* 
* @param publisherUrl
* The publisher_url
*/
public void setPublisherUrl(String publisherUrl) {
this.publisherUrl = publisherUrl;
}

}

创建食谱的Arraylist

ArrayList<Recipe> recipes =new ArrayList<>(); 

像这样填写你的arraylist

private void getRecipes(JSONObject response) {
        try {
            JSONArray recipes = response.getJSONArray("recipes");


            for (int i = 0; i < recipes.length(); i++) {
                JSONObject object = recipes.getJSONObject(i);
                Recipe recipe = new Recipe();
                recipe.setF2fUrl(object.getString("F2fUrl"));
                recipe.setImageUrl(object.getString("ImageUrl"));
                recipe.setPublisher(object.getString("Publisher"));
                recipe.setPublisherUrl(object.getString("PublisherUrl"));
                recipe.setRecipeId(object.getString("RecipeId"));
                recipe.setSocialRank(object.getDouble("SocialRank"));
                recipe.setSourceUrl(object.getString("SourceUrl"));
                recipe.setTitle(object.getString("Title"));
                recipesArrayList.add(recipe);
            }

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


    }

我认为这对你很有帮助