我传递的JSON文件列表未在Android应用程序中显示

时间:2019-04-18 11:18:59

标签: java android json retrofit

我正在传递此JSON http://www.mocky.io/v2/5cacde192f000078003a93bb

我正在尝试仅打印category_name

当我像http://www.mocky.io/v2/5cb859344c0000092ed3d4df那样传递没有数据列表的对象时,我无法获取数据列表

   private Category_name category_name;

   public Category_name getCategoryName() {
       return category_name;
   }
}

   public class Category_name {
       @SerializedName("category_name")
       public String name;

       public String getName() {
           return name;
       }
   }````

i can access that through the NewAdapter.java
with the following code

@Override
   public void onBindViewHolder(NewsViewHolder holder, int position) {
       Log.e("Its coming","NewAdapter");
       ApiObject apiObject = apiObjectList.get(position);
       holder.title.setText(apiObject.getCategoryName().getName());
   }

with the same  code I'm not able to get the data list 
@SerializedName("data")
   public List<Data> data;

   public List<Data> getData() {
       return data;
   }

  public class Data {
   @SerializedName("details")
   private Category_name category_name;

   public Category_name getCategoryName() {
       return category_name;
   }
}

   public class Category_name {
       @SerializedName("category_name")
       public String name;

       public String getName() {
           return name;
       }
   }

@Override
   public void onBindViewHolder(NewsViewHolder holder, int position) {
       Log.e("Its coming","NewAdapter");
       ApiObject apiObject = apiObjectList.get(position);
         holder.title.setText(apiObject.getData().getCategoryName().getName());

   }
I'm not able to access the getCategoryName();

Please help thanks in advance

2 个答案:

答案 0 :(得分:0)

使用json 2 pojo转换创建正确的json数据模型类  http://www.jsonschema2pojo.org/

将整个示例对象传递给适配器构造器。

答案 1 :(得分:0)

我认为您需要根据JSON响应遵循以下POJO解析方式。

 public class Data{
 @Serialization("status")
 @Expose
 private String status;
 @Serialization("data")
 @Expose
 private List<MyData> data;

然后

public class MyData{
@Serialization("details")
@Expose
private List<Details> getDetails();
@Serialization("product_count")
@Expose
private String Product_count;
@Serialization("products")
@Expose
private List<Products> getProducts();
//setter and getters
}

详细信息POJO

Public class Details{
@Serialization("category_id")
@Expose
private String category_id;
@Serialization("category_name")
@Expose
private String category_name;
@Serialization("category_icon")
@Expose
private String category_icon;
//setter and getters
}

产品POJO

Public class Products{
@Serialization("product_id")
@Expose
private String product_id;
@Serialization("product_name")
@Expose
private String product_name;
@Serialization("product_image")
@Expose
private String product_icon;
etc
//setter and getters
}