gpt4 book ai didi

java - 将 JSON 映射到 Java 对象返回空值

转载 作者:行者123 更新时间:2023-11-30 08:40:56 24 4
gpt4 key购买 nike

我想像这样解析 json 对象:

{ 
"Count" : 1,
"Data" : [
{
"ContactID" : 1567993182,
"Email" : "enamdimensi@localhost.com",
"Action" : "unsub",
"Name" : "",
"Properties" : {}
}
],
"Total" : 1
}

到这个java对象。

public class Response {
@JsonProperty("Status")
private String status;
@JsonProperty("Data")
private List<DataResponse> data;
@JsonProperty("Total")
private Integer total;
@JsonProperty("Count")
private Integer count;

public MailjetResponse() {
super();
}

........ setter and getter .......
}

class DataResponse {
@JsonProperty("ContactID")
private String contactId;
@JsonProperty("Name")
private String name;
@JsonProperty("Email")
private String email;
@JsonProperty("Action")
private String action;
@JsonProperty("Properties")
private Map<String, Object> properties;

public DataResponse() {
super();
}
....... setter and getter .....
}

我用 Jackson 做的,这是我的代码:

final ObjectMapper mapper = new ObjectMapper();
MailjetResponse response = mapper.readValue(content, Response.class);

但是,如果我调试响应,所有字段 Response 都是空的。

response [Status=null, Data=null, Total=null, Count=null]

我的代码有问题吗?

更新代码:响应类

public class Response {
@JsonProperty("Status")
private String status;
@JsonProperty("Data")
private List<DataResponse> data;
@JsonProperty("Total")
private Integer total;
@JsonProperty("Count")
private Integer count;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Integer getTotal() {
return total;
}

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

public Integer getCount() {
return count;
}

public void setCount(Integer count) {
this.count = count;
}

@Override
public String toString() {
return "MailjetResponse [status=" + status + ", data=" + data
+ ", total=" + total + ", count=" + count + "]";
}
}

数据响应类

public class DataResponse {
@JsonProperty("ContactID")
private String contactId;
@JsonProperty("Name")
private String name;
@JsonProperty("Email")
private String email;
@JsonProperty("Action")
private String action;
@JsonProperty("Properties")
private Map<String, Object> properties;


public String getContactID() {
return contactId;
}


public void setContactID(String contactID) {
contactId = contactID;
}

public String getName() {
return name;
}


public void setName(String name) {
name = name;
}

public String getEmail() {
return email;
}


public void setEmail(String email) {
email = email;
}

public String getAction() {
return action;
}

public void setAction(String action) {
action = action;
}


@Override
public String toString() {
return "DataResponse [contactId=" + contactId + ", name=" + name
+ ", email=" + email + ", action=" + action + ", properties="
+ properties + "]";
}
}

结果是这样的:

response MailjetResponse [status=null, data=[DataResponse [contactId=1567993182, name=null, email=null, action=null, properties={}]], total=1, count=1]

最佳答案

    I have tried your example and used setter only and got email field populated after deserialisation of json.I could not see any other issue.

Below is the code I have tried :

public class Response {
@JsonProperty("Status")
private String status;
@JsonProperty("Data")
private List<DataResponse> data;
@JsonProperty("Total")
private Integer total;
@JsonProperty("Count")
private Integer count;

public void setStatus(String status) {
this.status = status;
}

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

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

public void setCount(Integer count) {
this.count = count;
}
}


public class DataResponse {
@JsonProperty("ContactID")
private String contactId;
@JsonProperty("Name")
private String name;
@JsonProperty("Email")
private String email;
@JsonProperty("Action")
private String action;
@JsonProperty("Properties")
private Map<String, Object> properties;

public void setContactId(String contactId) {
this.contactId = contactId;
}

public void setName(String name) {
this.name = name;
}

public void setEmail(String email) {
this.email = email;
}

public void setAction(String action) {
this.action = action;
}

public void setProperties(Map<String, Object> properties) {
this.properties = properties;
}
}


final ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
final Response response = mapper.readValue(message(), Response.class);

我更喜欢在构造函数上注释的 Jsoncreator。

关于java - 将 JSON 映射到 Java 对象返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361203/

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