【问题标题】:I'm passing a list of JSON file is not getting display in the Android application我传递的 JSON 文件列表未在 Android 应用程序中显示
【发布时间】:2019-04-18 11:18:58
【问题描述】:

我正在传递这个 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

【问题讨论】:

  • 对于你的新 json,它应该是 apiObject.getDetails().getCategoryName。另外你是如何解析你的 json 的?
  • 在 Json Category_name 中是详细信息对象内的字符串变量。和您的模型类,您创建 Category_name 类。
  • 根据您的 JSON 响应,您通过 POJO 解析的方式是错误的,或者您应该根据您的 POJO 更改您的 JSON 响应。
  • -@VivekMishra 我无法通过 apiobject 访问 getDetails

标签: java android json retrofit


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    我认为您需要根据您的 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
    }
    

    【讨论】:

    • 我应该在哪里使用@SerializedName("data")
    • 是的,我做的完全一样,我创建了 getData();在 MyData 类中的 Data 类 getDetails() 中,当我在其他类中创建 Data 类的对象时,我无法使用 getDetails(),例如数据是对象 data.getData().getDetails() 出现错误跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多