问题描述
我使用翻新从API接收以下格式的JSON数据:
{
"id": "8dfa4c16-60d7-43ed-9292-21f12a4b7f48","name": "Salon Ultra","lat": 0.36992,"lng": 32.668261,"address": "Level 3,Garden City","opening_time": "09:00:00","closing_time": "20:00:00","rating": 4,"whatsapp_contact": "+256701882716","primary_contact": "09:00:00","secondary_contact": "09:00:00","img_url": "hair.jpg","services": [
{
"service_id": "1e3b47f5-f915-410d-9a76-a018e83ff4b1","service_name": "Classic Locs","service_price": "150000","s_img_url": "img_locs.jpg","s_service_provider_id": "8dfa4c16-60d7-43ed-9292-21f12a4b7f48","s_reviews_count": null,"s_service_category_id": 1,"updated_at": "2020-10-03 15:13:08","created_at": "2020-10-03 15:13:08"
},{
"service_id": "2f384780-110f-4307-b1de-f4070bce2da9","service_name": "Straightened Natural","service_price": "20000","s_img_url": "img_natural_straight.jpg","updated_at": "2020-10-03 15:13:54","created_at": "2020-10-03 15:13:54"
},{
"service_id": "31fbfb23-60ba-4704-9e6a-19047bdb0a14","service_name": "Crochet Mid Length","service_price": "120000","s_img_url": "img_crochet.jpg","updated_at": "2020-09-25 21:39:20","created_at": "2020-09-25 21:30:49"
},{
"service_id": "b8a288ec-338d-4472-945e-60bb9ee7e7bd","service_name": "Mid-length Bob","service_price": "60000","s_img_url": "img_bob.jpg","updated_at": "2020-10-03 15:11:53","created_at": "2020-10-03 15:11:53"
},{
"service_id": "c8dec22a-2cd4-43e5-9664-64bf7364cd8a","service_name": "Wild Goddess weave","service_price": "70000","s_img_url": "img_wild.jpg","updated_at": "2020-10-03 15:13:26","created_at": "2020-10-03 15:13:26"
}
],"categories": [
{
"service_category_id": 1,"parent_service_category_id": null,"sc_name": "Hair","sc_img_url": "hair.jpg","updated_at": "2020-09-24 20:21:50","created_at": "2020-09-24 20:21:50","pivot": {
"service_provider_id": "8dfa4c16-60d7-43ed-9292-21f12a4b7f48","service_category_id": 1
}
},{
"service_category_id": 3,"sc_name": "Hair Products","sc_img_url": "hair-products.jpg","service_category_id": 3
}
}
],"products": [
{
"product_id": "08e2c93e-60b9-4cb6-be7c-ff3e7cbf3bce","product_name": "Cantu Hair Conditioner","product_old_price": null,"product_price": "32000","p_img_url": "img_conditioner.jpg","p_reviews_count": null,"p_service_provider_id": "8dfa4c16-60d7-43ed-9292-21f12a4b7f48","p_service_category_id": 1,"updated_at": "2020-09-25 21:10:56","created_at": "2020-09-25 21:03:23"
}
],"reviews": []
}
我已经使用JSON Schema to POJO来生成我的Model类,如下所示:
public class ServiceProviderResponse {
@Serializedname("id")
@Expose
private String id;
@Serializedname("name")
@Expose
private String name;
@Serializedname("lat")
@Expose
private Double lat;
@Serializedname("lng")
@Expose
private Double lng;
@Serializedname("address")
@Expose
private String address;
@Serializedname("opening_time")
@Expose
private String openingTime;
@Serializedname("closing_time")
@Expose
private String closingTime;
@Serializedname("rating")
@Expose
private Integer rating;
@Serializedname("whatsapp_contact")
@Expose
private String whatsappContact;
@Serializedname("primary_contact")
@Expose
private String primaryContact;
@Serializedname("secondary_contact")
@Expose
private String secondaryContact;
@Serializedname("img_url")
@Expose
private String imgurl;
@Serializedname("services")
@Expose
private List<Service> services;
@Serializedname("categories")
@Expose
private List<ServiceCategory> categories;
@Serializedname("products")
@Expose
private List<Product> products;
@Serializedname("reviews")
@Expose
private List<ServiceProviderReview> reviews;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLng() {
return lng;
}
public void setLng(Double lng) {
this.lng = lng;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getopeningTime() {
return openingTime;
}
public void setopeningTime(String openingTime) {
this.openingTime = openingTime;
}
public String getClosingTime() {
return closingTime;
}
public void setClosingTime(String closingTime) {
this.closingTime = closingTime;
}
public Integer getrating() {
return rating;
}
public void setrating(Integer rating) {
this.rating = rating;
}
public String getWhatsappContact() {
return whatsappContact;
}
public void setWhatsappContact(String whatsappContact) {
this.whatsappContact = whatsappContact;
}
public String getPrimaryContact() {
return primaryContact;
}
public void setPrimaryContact(String primaryContact) {
this.primaryContact = primaryContact;
}
public String getSecondaryContact() {
return secondaryContact;
}
public void setSecondaryContact(String secondaryContact) {
this.secondaryContact = secondaryContact;
}
public String getimgurl() {
return imgurl;
}
public void setimgurl(String imgurl) {
this.imgurl = imgurl;
}
public List<Service> getServices() {
return services;
}
public void setServices(List<Service> services) {
this.services = services;
}
public List<ServiceCategory> getCategories() {
return categories;
}
public void setCategories(List<ServiceCategory> categories) {
this.categories = categories;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
public List<ServiceProviderReview> getReviews() {
return reviews;
}
public void setReviews(List<ServiceProviderReview> reviews) {
this.reviews = reviews;
}
}
子列表的模型示例:
public class ServiceCategory {
@Serializedname("id")
@Expose
private int id;
@Serializedname("parent_id")
@Expose
private int parent_id;
@Serializedname("name")
@Expose
private String name;
@Serializedname("img_url")
@Expose
private String img_url;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getParent_id() {
return parent_id;
}
public void setParent_id(int parent_id) {
this.parent_id = parent_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImg_url() {
return img_url;
}
public void setImg_url(String img_url) {
this.img_url = img_url;
}
}
在我的片段中,下面的行有效:
binding.setProvider(data);
正确填写了提供者的详细信息。
但是,即使我可以获取列表的大小并遍历其项目,也无法对数据嵌套列表中的数据使用getter方法。回收者视图也不会填充。
//Chip view creates two blank chips
List<ServiceCategory> categories = data.getCategories();
for(int i = 0; i<categories.size(); i++){
ServiceCategory category = categories.get(i);
Chip chip = new Chip(activity);
chip.setText(category.getName());
chip.setChipBackgroundColorResource(R.color.colorPrimary);;
chip.setTextColor(activity.getResources().getColor(R.color.colorWhite,activity.getTheme()));
chip.setTextAppearance(R.style.ChipTextAppearance);
categoryChipGroup.addView(chip);
}
解决方法
您反序列化 ServiceCategory 错误。
用以下代码替换您的 ServiceCategory 类:
public class ServiceCategory {
@SerializedName("service_category_id")
@Expose
private int id;
@SerializedName("parent_service_category_id")
@Expose
private int parentId;
@SerializedName("sc_name")
@Expose
private String scName;
@SerializedName("sc_img_url")
@Expose
private String scImageUrl;
@SerializedName("updated_at")
@Expose
private String updatedAt;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("pivot")
@Expose
private Pivot pivot;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getParentId() {
return parentId;
}
public void setParentId(int parentId) {
this.parentId = parentId;
}
public String getScName() {
return scName;
}
public void setScName(String scName) {
this.scName = scName;
}
public String getScImageUrl() {
return scImageUrl;
}
public void setScImageUrl(String scImageUrl) {
this.scImageUrl = scImageUrl;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public Pivot getPivot() {
return pivot;
}
public void setPivot(Pivot pivot) {
this.pivot = pivot;
}
public class Pivot {
@SerializedName("service_provider_id")
@Expose
private String providerID;
@SerializedName("service_category_id")
@Expose
private String serviceCategoryID;
public String getProviderID() {
return providerID;
}
public void setProviderID(String providerID) {
this.providerID = providerID;
}
public String getServiceCategoryID() {
return serviceCategoryID;
}
public void setServiceCategoryID(String serviceCategoryID) {
this.serviceCategoryID = serviceCategoryID;
}
}
}