如何为这个 JSON 文件创建 Retrofit 的模型?

huangapple go评论90阅读模式
英文:

How can i make a model for this Json file for Retrofit?

问题

Json 文件如下:

{
  "data": [
    {
      "name": "key",
      "error": "key 不合法"
    },
    {
      "name": "package_name",
      "error": "包名不合法"
    }
  ],
  "success": false,
  "message": "信息不合法"
}

我有一个 BaseModel,它包含了 "success"、"message"、"data" 这些字段,所有的响应都是从这个类继承的。但是 "data" 对于服务器的每个响应都是不同的。

我目前已经做了如下内容:

public class BaseModel {
    private Object data;
    private boolean success;
    private String message;
}

在这种错误情况下,"data" 将会被转换为一个 DataError 数组:

public class DataError {
    private String name;
    private String error;
}

然而,我得到了一个错误,错误信息为:

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为 com.example.mapp.Model.DataError

我尝试将响应的 "data" 转换为以下的 List:

List<DataError> dataError = (List<DataError>) response.body().getData();
textView.append("错误:" + dataError.get(0).getError());
英文:

The Json file is:

{
  &quot;data&quot;: [
    {
      &quot;name&quot;: &quot;key&quot;,
      &quot;error&quot;: &quot;key is not valid&quot;
    },
    {
      &quot;name&quot;: &quot;package_name&quot;,
      &quot;error&quot;: &quot;package name is not valid&quot;
    }
  ],
  &quot;success&quot;: false,
  &quot;message&quot;: &quot;information is not valid&quot;
}

I've got a BaseModel which has "success","message", "data" and all of my responds are extended from this Class.
But "data is different for each response from the server.

I've made this so far:

public class BaseModel{
    private Object data;
    private boolean success;
    private String message;
}

which data for this case of error will cast to an array of DataError:

public class DataError{
        private String name;
        private String error;
}

And i get an error which tells me :

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to com.example.mapp.Model.DataError

List&lt;DataError&gt; dataError = (List&lt;DataError&gt;)response.body().getData();
textView.append(&quot;Error:&quot;+ dataError.get(0).getError());

答案1

得分: 0

你在解析时需要一个 List&lt;DataError&gt;,尽管它也可以解析为一个 Map。在 BaseModel 泛型中创建 data,这样你可以在其他类中重用它。将其命名为 BaseListModel,因为它返回一个列表。

public class BaseListModel&lt;T&gt;{
private List&lt;T&gt; data;
private boolean success;
private String message;
}

现在你可以使 API 调用返回 BaseListModel&lt;DataError&gt;

对于解析对象类型的响应,你可以创建另一个基础类。

public class BaseModel&lt;T&gt;{
private T data;
private boolean success;
private String message;
}
英文:

You need to have a List&lt;DataError> when parsing, Although it can also parse as a Map. Create data in BaseModel Generic this way you can reuse it other classes as well. naming it to BaseListModel cause it return a list.

public class BaseListModel&lt;T&gt;{
private List&lt;T&gt; data;
private boolean success;
private String message;
}

Now you can make the API call return BaseListModel&lt;DataError&gt; .

For parsing Object type responses you can create other base class .

 public class BaseModel&lt;T&gt;{
 private T data;
 private boolean success;
 private String message;
}

huangapple
  • 本文由 发表于 2020年9月12日 19:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63859918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定