将Volley的JSONObject响应转换为POJO

时间:2017-04-12 10:13:01

标签: java android json gson

在一个Android项目中,我使用volley来与API进行通信。 在其中一个请求中,我获得了带有此信息的JSONObject响应

{
"RgstrUsrRspn": {
    "Hdr": {
        "XchgId": "7403AC2E976D46968F6B4839E2FCB7A6",
        "DtTm": "2017-04-12T09:48:17.856+0000"
    },
    "RgstrUsr": {
        "Rslt": {
            "RsltStts": "DECL",
            "RsltRsn": ""
        }
    }
}

}

然后我想把它转换成这个类的Object:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "RegisterUserResponse1Document", propOrder = {
    "rgstrUsrRspn"
})
public class RegisterUserResponse1Document {

    @XmlElement(name = "RgstrUsrRspn", required = true)
    protected RegisterUserResponseV01 RgstrUsrRspn;
//...
}

这些是子类

public class RegisterUserResponseV01 {

            @XmlElement(name = "Hdr", required = true)
            protected Header2 Hdr;
            @XmlElement(name = "RgstrUsr", required = true)
            protected RegisterUserResponse1 RgstrUsr;
        //...
}


public class Header2 {

            @XmlElement(name = "XchgId", required = true)
            protected String xchgId;
            @XmlElement(name = "DtTm", required = true)
            protected Date dtTm;
            @XmlElement(name = "NxtFlow")
            protected NextFlowCode nxtFlow;
        //...
}

public class RegisterUserResponse1 {

        @XmlElement(name = "TknData")
        protected Token TknData;
        @XmlElement(name = "Rslt", required = true)
        protected Result1 Rslt;
    //...
}

public class Result1 {

    @XmlElement(name = "RsltStts", required = true)
    protected ResultStatus RsltStts;  //this is an enum
    @XmlElement(name = "RsltRsn", required = true)
    protected String RsltRsn;
//...
}

当我从截击中获得成功回应时,我会触发此听众

@Override
            public void notifySuccess(String requestType, JSONObject response) {

                Log.e("RESPONSE Success",response.toString());
                Gson gson = new Gson();
                RegisterUserResponse1Document registerUserResponse1Document1 = (RegisterUserResponse1Document)JsonBuilder.fromJson(response.toString(),RegisterUserResponse1Document.class);
                RegisterUserResponse1Document registerUserResponse1Document2 = gson.fromJson(response.toString(),RegisterUserResponse1Document.class);
//....
}

registerUserResponse1Document1registerUserResponse1Document2都填充了空值。我应该使用其他方法将响应转换为RegisterUserResponse1Document吗?

修改

JsonBuilder只是一个带有一些辅助方法的类,比如这个

public static Object fromJson(String json, Class type){

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingStrategy(FieldNamingPolicy.IDENTITY);
        gsonBuilder.setDateFormat(DateFormat);
        Gson gson = gsonBuilder.serializeNulls().create();
        return gson.fromJson(json, type);
}

编辑2

所有课程都有getter和setter,我只是省略了它们以缩短文本。

1 个答案:

答案 0 :(得分:0)

确保每个类属性都有正确的getter和setter,因为GSON通过反射将JSON转换为Object。

无论如何,此代码显示适合您:

-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

@SerializedName("RgstrUsrRspn")
@Expose
private RgstrUsrRspn rgstrUsrRspn;

public RgstrUsrRspn getRgstrUsrRspn() {
return rgstrUsrRspn;
}

public void setRgstrUsrRspn(RgstrUsrRspn rgstrUsrRspn) {
this.rgstrUsrRspn = rgstrUsrRspn;
}

}
-----------------------------------com.example.Hdr.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Hdr {

@SerializedName("XchgId")
@Expose
private String xchgId;
@SerializedName("DtTm")
@Expose
private String dtTm;

public String getXchgId() {
return xchgId;
}

public void setXchgId(String xchgId) {
this.xchgId = xchgId;
}

public String getDtTm() {
return dtTm;
}

public void setDtTm(String dtTm) {
this.dtTm = dtTm;
}

}
-----------------------------------com.example.RgstrUsr.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class RgstrUsr {

@SerializedName("Rslt")
@Expose
private Rslt rslt;

public Rslt getRslt() {
return rslt;
}

public void setRslt(Rslt rslt) {
this.rslt = rslt;
}

}
-----------------------------------com.example.RgstrUsrRspn.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class RgstrUsrRspn {

@SerializedName("Hdr")
@Expose
private Hdr hdr;
@SerializedName("RgstrUsr")
@Expose
private RgstrUsr rgstrUsr;

public Hdr getHdr() {
return hdr;
}

public void setHdr(Hdr hdr) {
this.hdr = hdr;
}

public RgstrUsr getRgstrUsr() {
return rgstrUsr;
}

public void setRgstrUsr(RgstrUsr rgstrUsr) {
this.rgstrUsr = rgstrUsr;
}

}
-----------------------------------com.example.Rslt.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Rslt {

@SerializedName("RsltStts")
@Expose
private String rsltStts;
@SerializedName("RsltRsn")
@Expose
private String rsltRsn;

public String getRsltStts() {
return rsltStts;
}

public void setRsltStts(String rsltStts) {
this.rsltStts = rsltStts;
}

public String getRsltRsn() {
return rsltRsn;
}

public void setRsltRsn(String rsltRsn) {
this.rsltRsn = rsltRsn;
}

}