Json包含具有相同键的不同类型,如何使用gson解析

时间:2018-04-03 02:09:19

标签: android gson retrofit

我使用Retrofit2从服务获取数据并使用Gson解析数据。以下代码是我的服务给我的。

{
"code": 0,
"data": {
    "totalPages": 7,
    "pageSize": 4,
    "page": 1,
    "data": [
        {
            "hot": 341.52,
            "orgid": 47,
            "total": 195,
            "logo": "http://krplus-pic.b0.upaiyun.com/201803/14/d83653c509d914b7ad9b5d72102c1314.png",
            "org_name": "氪空间"
        },
        {
            "hot": 210.41,
            "orgid": 43,
            "total": 41,
            "logo": "https://krplus-pic.b0.upaiyun.com/201706/01/11ed013d0c14ae9b66bf644bda19cbad.png",
            "org_name": "微软加速器"
        },
        {
            "hot": 76.38,
            "orgid": 53,
            "total": 32,
            "logo": "https://krplus-pic.b0.upaiyun.com/201708/14/cb3e643e27e674d69fd2488b51151127.png",
            "org_name": "中欧校友加速器"
        },
        {
            "hot": 14.51,
            "orgid": 54,
            "total": 8,
            "logo": "https://krplus-pic.b0.upaiyun.com/201706/01/9b780946f4800479840cf934f83838a4.png",
            "org_name": "硬蛋科技"
        }
    ],
    "totalCount": 61
},
"msg": "操作成功!"

}

但是同一个键data有不同的类型,我怎样才能使用gson解析。由于现在直接使用onFailure方法,不要使用响应方法。 以下是我的BaseResponse

public class BaseResponse<T> {

private int code;
private String msg;
private T data;

public int getCode() {
    return code;
}

public void setCode(int code) {
    this.code = code;
}

public String getMsg() {
    return msg;
}

public void setMsg(String msg) {
    this.msg = msg;
}

public T getData() {
    return data;
}

public void setData(T data) {
    this.data = data;
}

}

以下是子响应bean:

public class ListTextBean {

private int totalPages;
private int pageSize;
private int page;
private int totalCount;
private List<DataBean> data;

public int getTotalPages() {
    return totalPages;
}

public void setTotalPages(int totalPages) {
    this.totalPages = totalPages;
}

public int getPageSize() {
    return pageSize;
}

public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
}

public int getPage() {
    return page;
}

public void setPage(int page) {
    this.page = page;
}

public int getTotalCount() {
    return totalCount;
}

public void setTotalCount(int totalCount) {
    this.totalCount = totalCount;
}

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

public void setData(List<DataBean> data) {
    this.data = data;
}

public static class DataBean {
    private double hot;
    private int orgid;
    private int total;
    private String logo;
    private String org_name;

    public double getHot() {
        return hot;
    }

    public void setHot(double hot) {
        this.hot = hot;
    }

    public int getOrgid() {
        return orgid;
    }

    public void setOrgid(int orgid) {
        this.orgid = orgid;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    public String getOrg_name() {
        return org_name;
    }

    public void setOrg_name(String org_name) {
        this.org_name = org_name;
    }
}

}

2 个答案:

答案 0 :(得分:0)

想法是像

一样遍历
for(int i = 0; i < data.length(); i++){
    //get each object inside the array
    JSONObject item = data.getJSONObject(i);

    //then do something here
}

然后得到孩子

{{1}}

答案 1 :(得分:0)

尝试这些课程

    public class BaseResponse {

@SerializedName("code")
@Expose
private Integer code;
@SerializedName("data")
@Expose
private Data data;
@SerializedName("msg")
@Expose
private String msg;

public Integer getCode() {
return code;
}

public void setCode(Integer code) {
this.code = code;
}

public Data getData() {
return data;
}

public void setData(Data data) {
this.data = data;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

}

下一个

    public class ListTextBean {

@SerializedName("totalPages")
@Expose
private Integer totalPages;
@SerializedName("pageSize")
@Expose
private Integer pageSize;
@SerializedName("page")
@Expose
private Integer page;
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("totalCount")
@Expose
private Integer totalCount;

public Integer getTotalPages() {
return totalPages;
}

public void setTotalPages(Integer totalPages) {
this.totalPages = totalPages;
}

public Integer getPageSize() {
return pageSize;
}

public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}

public Integer getPage() {
return page;
}

public void setPage(Integer page) {
this.page = page;
}

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

public void setData(List<Datum> data) {
this.data = data;
}

public Integer getTotalCount() {
return totalCount;
}

public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}

}

    public class Datum {

@SerializedName("hot")
@Expose
private Double hot;
@SerializedName("orgid")
@Expose
private Integer orgid;
@SerializedName("total")
@Expose
private Integer total;
@SerializedName("logo")
@Expose
private String logo;
@SerializedName("org_name")
@Expose
private String orgName;

public Double getHot() {
return hot;
}

public void setHot(Double hot) {
this.hot = hot;
}

public Integer getOrgid() {
return orgid;
}

public void setOrgid(Integer orgid) {
this.orgid = orgid;
}

public Integer getTotal() {
return total;
}

public void setTotal(Integer total) {
this.total = total;
}

public String getLogo() {
return logo;
}

public void setLogo(String logo) {
this.logo = logo;
}

public String getOrgName() {
return orgName;
}

public void setOrgName(String orgName) {
this.orgName = orgName;
}

}

并且您的改装电话应该是

Call<BaseResponse> call1=request.signupPost(jsonObject);

注意:在响应回调,onfailure回调和api接口中将响应类型更改为BaseResponse