我正在尝试将数据解析到一个recyclerview中,我在期望得到一些帮助的情况下修复JSONArray / JSONObject时遇到了一些问题,但此刻我在Onresponse
中的操作上有些失落,原来的generatePhonesList(response.body())
无法正常工作。
这是我的json,我正在尝试解析array results[]
中的数据:
{
"success": true,
"metadata": {
"sort": "POPULARITY",
"total_products": 20,
"title": "Phones & Tablets",
"results": [
{
"sku": "1",
"name": "Samsung Galaxy S9",
"brand": "Samsung",
"max_saving_percentage": 30,
"price": 53996,
"special_price": 37990,
"image": "https://cdn2.gsmarena.com/vv/bigpic/samsung-galaxy-s9-.jpg",
"rating_average": 5
},
MainActivity(调用和创建Recyclerview):
GetPhoneDataService service = RetrofitInstance.getRetrofitInstance().create(GetPhoneDataService.class);
Call<APIReponse> call = service.getAllPhones();
call.enqueue(new Callback<APIReponse>() {
@Override
public void onResponse(Call<APIReponse> call, Response<APIReponse> response) {
generatePhonesList(response.body());
}
@Override
public void onFailure(Call<APIReponse> call, Throwable t) {
Log.e("eee" , "" + t.getMessage());
}
});
}
private void generatePhonesList(List<Result> phonesList){
recyclerView = findViewById(R.id.recyclerView);
adapter = new PhonesAdapter(phonesList,this);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
这是在jsonschema2pojo中创建的POJO类:
public class APIReponse {
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("metadata")
@Expose
private Metadata metadata;
public Boolean getSuccess() {
return success;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public Metadata getMetadata() {
return metadata;
}
public void setMetadata(Metadata metadata) {
this.metadata = metadata;
}
}
2节课
public class MetaData {
@SerializedName("sort")
@Expose
private String sort;
@SerializedName("total_products")
@Expose
private Integer totalProducts;
@SerializedName("title")
@Expose
private String title;
@SerializedName("results")
@Expose
private List<Result> results = null;
public String getSort() {
return sort;
}
public void setSort(String sort) {
this.sort = sort;
}
public Integer getTotalProducts() {
return totalProducts;
}
public void setTotalProducts(Integer totalProducts) {
this.totalProducts = totalProducts;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
}
3类:
public class Result {
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("name")
@Expose
private String name;
@SerializedName("brand")
@Expose
private String brand;
@SerializedName("max_saving_percentage")
@Expose
private Integer maxSavingPercentage;
@SerializedName("price")
@Expose
private Integer price;
@SerializedName("special_price")
@Expose
private Integer specialPrice;
@SerializedName("image")
@Expose
private String image;
@SerializedName("rating_average")
@Expose
private Integer ratingAverage;
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public Integer getMaxSavingPercentage() {
return maxSavingPercentage;
}
public void setMaxSavingPercentage(Integer maxSavingPercentage) {
this.maxSavingPercentage = maxSavingPercentage;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
public Integer getSpecialPrice() {
return specialPrice;
}
public void setSpecialPrice(Integer specialPrice) {
this.specialPrice = specialPrice;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Integer getRatingAverage() {
return ratingAverage;
}
public void setRatingAverage(Integer ratingAverage) {
this.ratingAverage = ratingAverage;
}
}
答案 0 :(得分:1)
如果您密切关注,response.body()
将为您提供APIResponse
类。但是您需要的是List<Result>
。为此,请尝试response.body().getMetadata().getResults()
这应该为您提供所需的输出。
答案 1 :(得分:1)
您正在将APIReponse
模型传递给generatePhonesList(List<Result> phonesList)
函数。您只需在此函数中传递结果列表即可。
替换此:
generatePhonesList(response.body());
具有:
generatePhonesList(response.body().getMetadata().getResults());
getMetadata()和getResults()是元数据模型和List的getter函数。