故事:我想阅读一次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'}
答案 0 :(得分:0)
可以在同一级别上对不同名称的对象进行解析。定义@SerializedName(value="name1", alternate={"name2", "name3"}) String b;
的一种可能方式是定义对象的备用名称。但是请注意,此对象需要具有相同的数据。在您的情况下,由于json在所有情况下都包含这两个字段,因此在所有情况下都将解析“ id” ,“ name” 。
另一方面,当您要解析所有字段时,最好使用自定义 JsonDeserializer 。您可以在此处找到如何为GSON库编写自己的反序列化器的示例:Deserializer
答案 1 :(得分:0)
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;
}
}