objectMapper.readValue不适用于此JSON,是否有问题?

时间:2019-05-02 14:09:57

标签: json jackson objectmapper

从silverpop解析结果时,我们使用的是objectMapper。

响应如下:

{
  "meta": {
    "attributes": {},
    "generalErrors": [
      "No contact found"
    ],
    "fieldErrors": {},
    "links": [],
    "nextPageUrl": null
  },
  "data": null
}

以下代码似乎无法对此进行解析:

String str = "{\"meta\":{\"attributes\":{},\"generalErrors\":[\"No contact found\"],\"fieldErrors\":{},\"links\":[],\"nextPageUrl\":null},\"data\":null}";
ObjectMapper objectMapper = ObjectMapperFactory.getDefaultObjectMapper();
ApiResponse apiResponse = objectMapper.readValue(str, 
ApiResponse.class);
log.info("apiResponse: " + apiResponse);

ApiResponse.java:

public class ApiResponseDO extends HttpResponse {
   private RestApiMeta meta;
   private RestApiData[] data;
   public ApiResponseDO() { }
   public RestApiMeta getMeta() {return meta;}
   public void setMeta(RestApiMeta meta) { this.meta = meta; }
   public RestApiData[] getData() {   return data; }
   public void setData(RestApiData[] data) {  this.data = data; }

}

RestApiMeta.java

public class RestApiMeta {
    Object attributes;  
    String[] generalErrors;
    Object fieldErrors; 
    Object[] links;   
    Object nextPageUrl;

    public RestApiMeta() {     }
    public Object getAttributes() { return attributes; }
    public void setAttributes(Object attributes) { this.attributes = attributes;   }
    public String[] getGeneralErrors() { return generalErrors; }
    public void setGeneralErrors(String[] generalErrors) {           this.generalErrors = generalErrors;  }
    public Object getFieldErrors() {  return fieldErrors;  }
    public void setFieldErrors(Object fieldErrors) {         this.fieldErrors = fieldErrors;   }
    public Object[] getLinks() {  return links;  }
    public void setLinks(Object[] links) { this.links = links; }
    public String getNextPageUrl() { return nextPageUrl;  }
    public void setNextPageUrl(String nextPageUrl) { this.nextPageUrl = nextPageUrl; }

输出看起来像这样:

apiResponse: meta=attributes={},generalErrors=<null>,fieldErrors=<null>,links={},nextPageUrl=<null>,,data=<null>

即它返回了一个空的(新的)对象。没有错误或异常。

有什么想法吗?

0 个答案:

没有答案