gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:17:51 26 4
gpt4 key购买 nike

我正在开发休息客户端程序,其中我使用 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 中,我仅访问我需要的字段。但作为回应,我总是将数据数组设为空。没有错误。所以我真的不明白到底出了什么问题。谁能帮我解决这个问题吗?

Info [Status=success, Data=[]]

最佳答案

您的 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]]]]

关于java - JSON 响应中的 Json 数组始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48806876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com