JSON响应中的Json数组始终为null

时间:2018-02-15 12:05:37

标签: java json rest gson jersey-2.0

我正在研究rest客户端程序,我在其中使用gson解析json响应。我的json回复如下

{
    "Status": "success",

    "Data": [
        {
            "ID": "123",
            "Type": 0,
            "OperatorID": null,
            "DepartmentID": "128",
            "LanguageCode": "en-US",
            "WebsiteDefID": "160",
            "VisitID": "737",
        },
        {
            "ID": "737867804181437078",
            "Type": 0,
            "OperatorID": "1785",
            "DepartmentID": "200",
            "LanguageCode": "en-US",
            "WebsiteDefID": "160",
            "VisitID": "737",
            "CustomFields": {
                "Model": "ABCD",
                "Question": "ABCD"
            }
        }
    ]
}

Gson:

GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson= gsonBuilder.create();
Info info = gson.fromJson(inactiveChatMessages, Info.class);

我的InfoData Pojo课程如下:

public class Info {
    private String Status;

    private Data[] Data;

    public String getStatus() {
        return Status;
    }
    public void setStatus(String status) {
        Status = status;
    }

    public Data[] getData() {
        return Data;
    }
    public void setData(Data[] data) {
        Data = data;
    }

    @Override
    public String toString() {
        return "ChatInfo [Status=" + Status + ",  Data=" + Arrays.toString(Data) + "]";
    }
}

数据:

public class Data {
    private String ID;
    private String OperatorID;
    private String DepartmentID;


    private CustomFields CustomFields;

    public String getID() {
        return ChatID;
    }
    public void setID(String ID) {
        ChatID = chatID;
    }
    public String getOperatorID() {
        return OperatorID;
    }
    public void setOperatorID(String operatorID) {
        OperatorID = operatorID;
    }
    public String getDepartmentID() {
        return DepartmentID;
    }
    public void setDepartmentID(String departmentID) {
        DepartmentID = departmentID;
    }

    @Override
    public String toString() {
        return "Data [ID=" + ID + ", OperatorID=" + OperatorID + ", DepartmentID=" + DepartmentID + ", CustomFields=" + CustomFields
                + "]";
    }
}

在来自数组的Data pojo中,我只访问我需要的字段。但作为回应,我总是将数据数组作为null。没有错误。所以我真的不知道到底出了什么问题。有人可以帮我这个吗?

Info [Status=success, Data=[]]

1 个答案:

答案 0 :(得分:1)

您的Json无效

你有效的json。

{
    "Status": "success",

    "Data": [{
            "ID": "123",
            "Type": 0,
            "OperatorID": null,
            "DepartmentID": "128",
            "LanguageCode": "en-US",
            "WebsiteDefID": "160",
            "VisitID": "737"

        },
        {
            "ID": "737867804181437078",
            "Type": 0,
            "OperatorID": "1785",
            "DepartmentID": "200",
            "LanguageCode": "en-US",
            "WebsiteDefID": "160",
            "VisitID": "737",
            "CustomFields": {
                "Model": "ABCD",
                "Question": "ABCD"
            }

        }
    ]
}

对于json验证,您可以使用https://jsonlint.com/

否则你的代码工作正常。

这是输出

ChatInfo [Status=success,  Data=[Data [ID=123, OperatorID=null, DepartmentID=128, CustomFields=null], Data [ID=737867804181437078, OperatorID=1785, DepartmentID=200, CustomFields=[Model=ABCD,question=ABCD]]]]