Java 如何将此JSONArray存储到ArrayList中?
我从API Get调用的Java 如何将此JSONArray存储到ArrayList中?,java,android,json,arraylist,Java,Android,Json,Arraylist,我从API Get调用的JSONArray构建ArrayList时遇到一些问题。我收到一张空名单。我打电话的时候做错了什么 我试图接收的JSONArray是“recipes”。我只需要“title”和“image\u url” 方法检索JSONArray并将其存储到ArrayList中 public class JsonHelper { public List<Recipe> getRecipes(String jsonText) { List<Recipe>
JSONArray
构建ArrayList
时遇到一些问题。我收到一张空名单。我打电话的时候做错了什么
我试图接收的JSONArray
是“recipes
”。我只需要“title
”和“image\u url
”
方法检索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;
}}
任何帮助都将不胜感激。提前谢谢 我想这对你有用
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;
}
public ArrayList getRecipes(字符串jsonText){
ArrayList arra_list=新的ArrayList();
试一试{
JSONObject JSONObject=新的JSONObject(jsonText);
JSONArray recipes JSONArray=jsonObject.getJSONArray(“recipes”);
对于(int i=0;i
首先创建一个类名配方,如下所示
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;
}
}
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();
}
}
创建食谱的数组列表
ArrayList<Recipe> recipes =new ArrayList<>();
ArrayList recipes=new ArrayList();
像这样填写你的arraylist
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;
}
}
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();
}
}
private void getRecipes(JSONObject响应){
试一试{
JSONArray recipes=response.getJSONArray(“recipes”);
对于(int i=0;i
我认为这将非常清楚地帮助您这段代码的结果是什么?您想要什么?只需要将值转换为数组列表?实际问题是什么?发布实际错误对不起,伙计们,我是网站上的新成员。我发布了一些我用来进行API调用的方法的额外代码。问题是我收到一个空的ArrayList。我想我在JSONHelper中做错事了。我发现了问题所在。我的一节课上有个打字错误。我的结构和你给我的差不多!谢谢你的帮助!