如何使用改造获取json对象内的json数组

时间:2017-05-18 03:54:50

标签: android json

我第一次使用json数组,在json对象中使用retrofit。但我不知道如何访问它以及如何将其绑定到recyclerview和卡片视图。

{
"data": [
    {
        "id": 1,
        "Name": "Choc Cake",
        "Image": "1.jpg",
        "Category": "Meal",
        "Method": "",
        "Ingredients": [
            {
                "name": "1 Cup Ice"
            },
            {
                "name": "1 Bag Beans"
            }
        ]
    },

我的数据是这样的。
我想要检索&将它展示给recyclerview。 制作两个pojo类,一个包含所有数据,另一个包含数组列表 请有人告诉我如何做这件事。

1 个答案:

答案 0 :(得分:-1)

使用此pojo类

public class Datum {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("Name")
@Expose
private String name;
@SerializedName("Image")
@Expose
private String image;
@SerializedName("Category")
@Expose
private String category;
@SerializedName("Method")
@Expose
private String method;
@SerializedName("Ingredients")
@Expose
private List<Ingredient> ingredients = null;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getMethod() {
return method;
}

public void setMethod(String method) {
this.method = method;
}

public List<Ingredient> getIngredients() {
return ingredients;
}

public void setIngredients(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}

}
-----------------------------------com.example.Example.java-----------------------------------



public class Example {

@SerializedName("data")
@Expose
private List<Datum> data = null;

public List<Datum> getData() {
return data;
}

public void setData(List<Datum> data) {
this.data = data;
}

}
-----------------------------------com.example.Ingredient.java-----------------------------------


public class Ingredient {

@SerializedName("name")
@Expose
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}