【问题标题】:How do I store this JSONArray into an ArrayList?如何将此 JSONArray 存储到 ArrayList 中?
【发布时间】:2015-12-10 08:53:37
【问题描述】:

我在从 API Get 调用的 JSONArray 构建 ArrayList 时遇到了一些问题。我收到一个空列表。我在通话中做错了什么?

我尝试接收的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=");
}

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

【问题讨论】:

  • 这段代码的结果是什么?
  • 你只想要什么值转换成数组列表
  • 真正的问题是什么?
  • 发布实际错误
  • 对不起,我是网站上的新人。我发布了一些我用来进行 API 调用的方法的额外代码。问题是我收到一个空的 ArrayList。我想我在 JSONHelper 中尝试做错事。

标签: java android json arraylist


【解决方案1】:

首先像这样创建一个类名配方

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<Recipe> recipes =new 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();
        }


    }

我认为这会非常清楚地帮助你

【讨论】:

  • 我找到了问题所在。我的一堂课有错字。我的结构和你给我的差不多!感谢您的帮助!
【解决方案2】:

我认为它适合你

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

【讨论】:

    猜你喜欢
    • 2019-09-23
    • 2023-04-10
    • 2015-01-05
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多