[英]Multiple different nested objects in json to retrofit
故事:我想读一次Json页面。 没关系,我对此没有问题。我只能读取第一个单元格中的数据。 其他类中的其他单元格。 而且我通过使用http://www.jsonschema2pojo.org/具有所有类!重要! 每个不同的类型都包含不同的对象命名列表。
但是,如何通过改型库将每个单元格中的数据分配到不同的类?
public class FeaturedList{
//this my main class
@SerializedName("featured")
private List<FeaturedItem> featured;
@SerializedName("type")
private String type;
@SerializedName("title")
private String title;
public void setFeatured(List<FeaturedItem> featured){
this.featured = featured;
}
public List<FeaturedItem> getFeatured(){
return featured;
}
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(){
return
"FeaturedList{" +
"featured = '" + featured + '\'' +
",type = '" + type + '\'' +
",title = '" + title + '\'' +
"}";
}
restInsterface = ApiClient.getClient().create(RestInsterface.class);
Call<List<FeaturedList>> listCall;
listCall=restInsterface.getFeaturedList();
listCall.enqueue(new Callback<List<FeaturedList>>() {
@Override
public void onResponse(Call<List<FeaturedList>> call, Response<List<FeaturedList>> response) {
for (FeaturedList item:response.body()){
Log.i(TAG, "onResponse: "+item.toString());
}
}
@Override
public void onFailure(Call<List<FeaturedList>> call, Throwable t) {
}
});
}
MainActivity:onResponse:FeaturedList {featured ='[FeaturedItem {cover ='Cover {thumbnail ='Thumbnail {width ='270',url =' https ://cdn.vitrinova.com/images/jewelryafile-19eff39a-e6f9-4e56 -9bf1-58ee0fd7920b.jpg ',height ='135'}',width ='1080',medium ='Medium {width ='540',url =' https://cdn.vitrinova.com/images/jewelryafile- 27bc984d-08ff-43ba-9b6e-0b267ddb7c56.jpg ',height ='270'}' ......-- 继续
MainActivity:onResponse:FeaturedList {featured ='null',type ='new_products',title ='En YeniÜrünler'}
MainActivity:onResponse:FeaturedList {featured ='null',type ='categories',title ='Kategoriler'}
MainActivity:onResponse:FeaturedList {featured ='null',type ='collections',title ='Koleksiyonlar'}
MainActivity:onResponse:FeaturedList {featured ='null',type ='editor_shops',title ='EditörSeçimiVitrinler'}
MainActivity:onResponse:FeaturedList {featured ='null',type ='new_shops',title ='En Yeni Vitrinler'}
可以对同一级别的对象的不同命名进行解析。 可以通过一种方式定义@SerializedName(value="name1", alternate={"name2", "name3"}) String b;
含义定义对象的备用名称。 但是请注意,此对象需要具有相同的数据。 在您的情况下,它将在所有情况下解析“ id”和“ name” ,因为json在所有情况下均包含这两个字段。
另一方面,当您想解析所有字段时,最好使用自定义JsonDeserializer 。 您可以在此处找到如何为GSON库编写自己的反序列化器的示例: Deserializer
Okey我做到了,这是我的解决方案。 而且有效。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.