我正在使用改装和Gson转换器。我有两个结构相同的JSON,如下所示。
JSON 1
{"message":"ok",
"code":200,
"result":[{"name":"test"
"id":121
}]
}
JSON 2
{"message":"ok",
"code":200,
"result":[{"first_name":"test"
"last_name":"testing2"
"middle_name":"test123"
}]
}
为此,我创建了一个常见的模型类,如
public void CommonModel {
@SerializedName("code")
public int code;
@SerializedName("message")
public String message;
@SerializedName("result")
public ResultModel result;
public void ResultModel{
public List<JSON1> json1;
public List<JSON2> json2;
}
}
public void JSON1 {
@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
}
public void JSON2 {
@SerializedName("first_name")
public String firstName;
@SerializedName("last_name")
public String lastName;
@SerializedName("middle_name")
public String middleName;
}
但它没有用。当我在杰克逊尝试相同的概念时,效果很好。我想重用CommonModel来获取来自webservice的响应。如果任何人有解决方案,请添加评论
答案 0 :(得分:3)
创建如下的通用模型
public class Common {
@SerializedName("message")
private String message;
@SerializedName("code")
private String code;
public String getMessage() {
return message;
}
public String getCode() {
return code;
}
}
为Json创建以下模型一个扩展此类的公共模型
public class JsonOne extends Common {
@SerializedName("result")
private List<JsonObjectOne> jsonObjectOneList;
public List<JsonObjectOne> getJsonObjectOneList() {
return jsonObjectOneList;
}
public void setJsonObjectOneList(List<JsonObjectOne> jsonObjectOneList) {
this.jsonObjectOneList = jsonObjectOneList;
}
}
public class JsonObjectOne {
@SerializedName("name")
private String name;
@SerializedName("id")
private String id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
为Json创建模型两个扩展此类的公共模型
public class JsonTwo extends Common {
@SerializedName("result")
private List<JsonObjectTwo> jsonObjectTwoList;
public List<JsonObjectTwo> getJsonObjectTwoList() {
return jsonObjectTwoList;
}
public void setJsonObjectTwoList(List<JsonObjectTwo> jsonObjectTwoList) {
this.jsonObjectTwoList = jsonObjectTwoList;
}
}
public class JsonObjectTwo {
@SerializedName("first_name")
private String firstName;
@SerializedName("last_name")
private String lastName;
@SerializedName("middle_name")
private String middleName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
}
答案 1 :(得分:1)
我所看到的,你有两个解决方案。
1)使用不同的类:
public void ModelOne {
@SerializedName("code")
public int code;
@SerializedName("message")
public String message;
@SerializedName("result")
public List<ResultModel> result;
public void ResultModelOne {
@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
}
}
public void ModelTwo {
@SerializedName("code")
public int code;
@SerializedName("message")
public String message;
@SerializedName("result")
public List<ResultModel> result;
public void ResultModel {
@SerializedName("first_name")
public String firstName;
@SerializedName("last_name")
public String lastName;
@SerializedName("middle_name")
public String middleName;
}
}
2)使用一个类,但在响应中解析:
public void Model {
@SerializedName("code")
public int code;
@SerializedName("message")
public String message;
@SerializedName("result")
public List<JSONObject> result;
}
public void ModelResultOne {
@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
}
public void ModelResultTwo {
@SerializedName("first_name")
public String firstName;
@SerializedName("last_name")
public String lastName;
@SerializedName("middle_name")
public String middleName;
}
然后:
List<ModelResultOne> list = new Gson.fromJson(result, new TypeToken<ArrayList<ModelResultOne.class>>() {}.getType());
或
List<ModelResultTwo> list = new Gson.fromJson(result, new TypeToken<ArrayList<ModelResultTwo.class>>() {}.getType());
答案 2 :(得分:0)
也许你可以试试这个:
public void Model {
@SerializedName("code")
public int code;
@SerializedName("message")
public String message;
@SerializedName("result")
public ResultModel result;
public void ResultModel {
@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
@SerializedName("first_name")
public String firstName;
@SerializedName("last_name")
public String lastName;
@SerializedName("middle_name")
public String middleName;
}
}
你的回答就是这样的。
Model res = response.body(); //response is of onResponse() method
ResultModel resultModel = res.getResultModel() //create getters and setters of model to get data
if (resultModel.getId != null){
// Do your handling for id and name here
} else {
// Do your handling for first name, middle name and last name
}
我希望这对你有用
答案 3 :(得分:0)
得到答案!!!!
public class CommonModel<T>
{
@SerializedName("code")
@Expose
private int code;
@SerializedName("message")
@Expose
private String message;
@SerializedName("result")
@Expose
private T result;
}
public void JSON1 {
@SerializedName("id")
public int id;
@SerializedName("name")
public String name;
}
public void JSON2 {
@SerializedName("first_name")
public String firstName;
@SerializedName("last_name")
public String lastName;
@SerializedName("middle_name")
public String middleName;
}
<强> ///////////////////////////////////////// 解 ///////////////////////////////////////// 强>
通话&GT; getSetupJsonOne();
通话&GT; getSetupJsonTwo();