2 回答

TA貢獻2011條經(jīng)驗 獲得超2個贊
好吧,我做到了,這是我的解決方案。它有效。
public class DataList{
@SerializedName("featured")
private List<FeaturedItem> featured;
@SerializedName("products")
private List<ProductsItem> products;
@SerializedName("categories")
private List<CategoriesItem> categories;
@SerializedName("collections")
private List<CollectionsItem> collections;
@SerializedName("shops")
private List<ShopsItem> shops;
@SerializedName("type")
private String type;
@SerializedName("title")
private String title;
public List<FeaturedItem> getFeatured() {
return featured;
}
public void setFeatured(List<FeaturedItem> featured) {
this.featured = featured;
}
public List<ProductsItem> getProducts() {
return products;
}
public void setProducts(List<ProductsItem> products) {
this.products = products;
}
public List<CategoriesItem> getCategories() {
return categories;
}
public void setCategories(List<CategoriesItem> categories) {
this.categories = categories;
}
public List<CollectionsItem> getCollections() {
return collections;
}
public void setCollections(List<CollectionsItem> collections) {
this.collections = collections;
}
public List<ShopsItem> getShops() {
return shops;
}
public void setShops(List<ShopsItem> shops) {
this.shops = shops;
}
public void setType(String type){
this.type = type;
}
public String getType(){
return type;
}
public void setTitle(String title){
this.title = title;
}
public String getTitle(){
return title;
}
@Override
public String toString(){
if(type.equals("featured")){
return
"Featured Olanlar{" +
"featured = '" + featured + '\'' +
",type = '" + type + '\'' +
",title = '" + title + '\'' +
"}";
}
else if(type.equals("new_products")){
return
"En Yeni ürünler{" +
"products = '" + products + '\'' +
",type = '" + type + '\'' +
",title = '" + title + '\'' +
"}";
}
else if(type.equals("categories")){
return
"Kategoriler{" +
"categories = '" + categories + '\'' +
",type = '" + type + '\'' +
",title = '" + title + '\'' +
"}";
}
else if(type.equals("collections")){
return
"Koleksiyonlar{" +
"collections = '" + collections + '\'' +
",type = '" + type + '\'' +
",title = '" + title + '\'' +
"}";
}
else if(type.equals("editor_shops")){
return
"Edit?r Se?imi Vitrinler{" +
"shops = '" + shops + '\'' +
",type = '" + type + '\'' +
",title = '" + title + '\'' +
"}";
}
else if(type.equals("new_shops")){
return
"En Yeni Vitrinler{" +
"shops = '" + shops + '\'' +
",type = '" + type + '\'' +
",title = '" + title + '\'' +
"}";
}
return null;
}
}

TA貢獻1780條經(jīng)驗 獲得超1個贊
可以解析同一級別的對象的不同命名。在一種方式中,可以定義@SerializedName(value="name1", alternate={"name2", "name3"}) String b;
含義,定義對象的替代名稱。但要小心這個對象需要有相同的數(shù)據(jù)。在您的情況下,它將在所有情況下解析"id","name",因為 json 在所有情況下都包含這兩個字段。
另一方面,當您想要解析所有字段時,最好使用自定義JsonDeserializer。您可以在此處找到如何為 GSON 庫編寫自己的反序列化器的示例:反序列化器
添加回答
舉報