【问题标题】:Mapping JSON to Java Object return null value将 JSON 映射到 Java 对象返回空值
【发布时间】:2016-02-12 11:15:16
【问题描述】:

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

【问题讨论】:

    标签: java json jersey-client


    【解决方案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 在构造函数上注解。

    【讨论】:

      【解决方案2】:

      问题

      问题出在你的二传手上。

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

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

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

      Jackson 和带注释的字段访问

      Jackson 使用设置器,除非另有配置。更正设置器(例如,使用 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));
      

      另外:如果你更正了设置器,你可能会删除字段注释......选择最适合你的用例的东西。我更喜欢我的杰克逊序列化只使用字段,总是注释 - 或使用mixins

      【讨论】:

      • 感谢您的回复,我已经更新了代码,我得到了值,除了电子邮件值,仍然为空。我不知道为什么,我认为 email 或 contactId 是相同的,String。
      • 谢谢,我忘记在settter中使用“this”关键字了。
      • 我已经设置了这个,但它仍然是空的。
      猜你喜欢
      • 1970-01-01
      • 2013-04-17
      • 2017-01-01
      • 2021-02-17
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 2014-08-03
      相关资源
      最近更新 更多