将JSON映射到Java Object会返回null值

时间:2016-02-12 11:15:17

标签: java json jersey-client

我想像这样解析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 .....
}

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

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

但是,如果我调试响应,则所有字段Response都为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类

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

结果是这样的bocome:

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

2 个答案:

答案 0 :(得分:1)

问题

问题在于你的制定者。

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

这使得输入arg email到输入arg email(而不是字段this.email)的非限定分配。 它应该是:

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

杰克逊和带注释的现场访问

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

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

或更改映射器设置,例如

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));

另外:如果您更正了setter,您可以删除字段注释...选择最适合您的用例的内容。我更喜欢我的杰克逊序列化只使用字段,总是注释 - 或mixins

答案 1 :(得分: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。