将JSON映射到Java对象返回空值 [英] Mapping JSON to Java Object return null value

查看:442
本文介绍了将JSON映射到Java对象返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样解析json对象:

I want to parsing json object like this:

{ 
    "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 .....
}

我用杰克逊做的,这是我的代码:

I used Jackson to do that, and this is my code:

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

但是,如果我调试响应,则所有字段Response为null.

But, if I debug the response, all of the fields Response is null.

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 + "]";
} 
}

DataResponse类

DataResponse class

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]

推荐答案

问题

问题出在您的二传手.

Problem

The problem is in your setters.

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

这会导致从输入arg email到...输入arg email的非限定赋值(而不是字段this.email). 应该是:

This makes an unqualified assignment fron input arg email to ... input arg email (instead of the field this.email). It should be:

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

Jackson和带注释的字段访问

除非另有配置,否则杰克逊会使用设置器.请更正设置器(例如,使用IDE自动生成设置器)或删除它们并仅使用字段.为此,要么用

Jackson and annotated field access

Jackson uses setters unless configured otherwise. Either correct the setters (e.g. auto-generate them with IDE) or remove them and use fields only. To do that either annotate class with

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class DataResponse {

或更改映射器设置,例如

or change mapper settings, e.g.

ObjectMapper mapper  = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));

也:如果您更正了设置者,则可以删除字段注释...选择最适合您的用例的东西.我更喜欢让杰克逊序列化仅使用带有注释的字段完成,也可以使用 mixins .

Also: if you correct setters you may drop field annotations... Pick whatever is best for your use case. I prefer my jackson serialization to be done with just fields, always annotated - or with mixins.

这篇关于将JSON映射到Java对象返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆