无法从JSON获取对象

时间:2016-04-12 13:52:00

标签: java json serialization jackson jhipster

所以,我目前正在使用AWS lambda,我有一个奇怪的行为问题。 如果微服务返回一个对象列表,我可以通过将它们转换为List来获取所有项目。

JSON:

{
  "payload": [
      {
      "id": 35,
      "start": {
        "year": 2016,
        "month": "OCTOBER",
        "dayOfMonth": 10,
        "dayOfWeek": "MONDAY",
        "era": "CE",
        "dayOfYear": 284,
        "leapYear": true,
        "monthValue": 10,
        "chronology": {
          "id": "ISO",
          "calendarType": "iso8601"
        }
      },
      "end": {
        "year": 2016,
        "month": "OCTOBER",
        "dayOfMonth": 10,
        "dayOfWeek": "MONDAY",
        "era": "CE",
        "dayOfYear": 284,
        "leapYear": true,
        "monthValue": 10,
        "chronology": {
          "id": "ISO",
          "calendarType": "iso8601"
        }
      },
      "user": "admin"
    }
  ]  
}

并使用像这样的简单代码

return (List<Delegation>) response.getBody().getPayload();

我得到了整个清单。 ResponseClass很简单

public class ResponseClass <T> {

    private T payload;

    public ResponseClass() { }

    public void setPayload(T payload) {
        this.payload = payload;
    }

    public T getPayload() {
        return payload;
    }
}

但是当我在有效载荷中接收单个对象时会发生奇怪的事情

所以现在 JSON:

{
  "payload": {
    "id": 35,
    "start": {
      "year": 2016,
      "month": "OCTOBER",
      "dayOfMonth": 10,
      "dayOfWeek": "MONDAY",
      "era": "CE",
      "dayOfYear": 284,
      "leapYear": true,
      "monthValue": 10,
      "chronology": {
        "id": "ISO",
        "calendarType": "iso8601"
      }
    },
    "end": {
      "year": 2016,
      "month": "OCTOBER",
      "dayOfMonth": 10,
      "dayOfWeek": "MONDAY",
      "era": "CE",
      "dayOfYear": 284,
      "leapYear": true,
      "monthValue": 10,
      "chronology": {
        "id": "ISO",
        "calendarType": "iso8601"
      }
    },
    "user": "admin"
  }
}

同样的事情(旁边它现在已经不在列表中) 当我试图以同样的方式投射时

(Delegation) response.getBody().getPayload();

我遇到了类强制转换异常,说我无法将java.util.LinkedHashMap强制转换为我的类型。

然后我尝试使用Jackson mapper:

mapper.convertValue(response.getBody().getPayload(), Delegation.class);

但我收到错误

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected START_ARRAY: Expected array or string.
at [Source: N/A; line: -1, column: -1] (through reference chain: xxx.Delegation["start"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:216)
at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:962)
at xxx.util.JSR310LocalDateDeserializer.deserialize(JSR310LocalDateDeserializer.java:61)
at xxx.util.JSR310LocalDateDeserializer.deserialize(JSR310LocalDateDeserializer.java:17)

xxx仅用于保护隐私。 我使用的是由jhipster生成的代码。

委托类

public class Delegation {

    private Long id;

    private String user;

    private LocalDate start;

    private LocalDate end;
}

省略了setter和getters。

当我尝试做的时候会发生同样的错误 mapper.convertValue(input.get("start"), LocalDate.class);

其中输入转换为LocalDate(java.lang.IllegalArgumentException:意外的标记(START_OBJECT),预期的START_ARRAY:预期的数组或字符串。  在[资料来源:N / A; line:-1,column:-1])

1 个答案:

答案 0 :(得分:0)

在第一个JSON中,payload是一个数组,将转换为List。在第二个JSON中,payload是一个对象。您无法将其转换为List并不奇怪。您需要将该对象收集为一个Object,如果您希望将其添加到List,则可能将其添加到List