android-使用gson恢复模型的通用结构

我正在使用翻新和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;
}

但这不起作用.当我在Jackson尝试相同的概念时,它可以很好地工作.我想将CommonModel重用于Web服务的响应.如果有解决方案,请添加评论

解决方法:

如下创建通用模型

public class Common {
    @SerializedName("message")
    private String message;

    @SerializedName("code")
    private String code;

    public String getMessage() {
        return message;
    }

    public String getCode() {
        return code;
    }
}

为Json One创建以下模型在此类上扩展Common Model

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创建模型两个在此类上扩展Common Model

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;
    }
}
上一篇:java-gson-反序列化包含列表的对象


下一篇:java-无法解析Wikipedia的JSON响应