改造:使用一个元素反序列化数组时出错:“超出START_ARRAY标记”

时间:2015-04-01 16:05:33

标签: java json jackson retrofit robospice

我正在编写一款利用robospice,改装和Jackson 2.4.X的Android应用程序,

这是我试图反序列化的json,它是从休息服务中回来的。 "数据中的元素"返回的数组是我要反序列化的Notification对象。

  {
  "data": [
      {
        "uuid": "XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX",
        "AcctNo": "XXXXXXXXXX",
        "title": "Test Notification",
        "content": "Here is some content.",
        "date": "2015-03-24"
      }
  ]
  }

我首先将响应包装在NotificationResponse对象中:

@JsonRootName(value = "data")
@JsonIgnoreProperties(ignoreUnknown = true)
public class NotificationResponse {

   @JsonUnwrapped
   public Notification[] notifications;
}

通知对象如下所示:

  public class Notification {

      @JsonProperty("uuid")
      public String Uuid;

      @JsonProperty("AcctNo")
      public String acctNo;

      @JsonProperty("title")
      public String title;

      @JsonProperty("content")
      public String content;

      @JsonProperty("date")
      public String date;

  }

堆栈跟踪:

  04-01 11:51:17.326  18978-19463/com.xxxxx.android E/﹕ An exception occurred during request network execution :com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.xxxxx.android.model.NotificationResponse out of START_ARRAY token
              at [Source: retrofit.ExceptionCatchingTypedInput$ExceptionCatchingInputStream@8b4da58; line: 1, column: 2]
      retrofit.RetrofitError: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.xxxxx.android.model.NotificationResponse out of START_ARRAY token
              at [Source: retrofit.ExceptionCatchingTypedInput$ExceptionCatchingInputStream@8b4da58; line: 1, column: 2]
              at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:383)
              at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
              at java.lang.reflect.Proxy.invoke(Proxy.java:397)
              at $Proxy15.notifications(Unknown Source)
              at com.xxxxx.android.network.spice.NotificationSpiceRequest.loadDataFromNetwork(NotificationSpiceRequest.java:21)
              at com.xxxxx.android.network.spice.NotificationSpiceRequest.loadDataFromNetwork(NotificationSpiceRequest.java:9)
              at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:48)
              at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:150)
              at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:217)
              at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
              at java.lang.Thread.run(Thread.java:818)
       Caused by: retrofit.converter.ConversionException: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.xxxxx.android.model.NotificationResponse out of START_ARRAY token
              at [Source: retrofit.ExceptionCatchingTypedInput$ExceptionCatchingInputStream@8b4da58; line: 1, column: 2]
              at retrofit.converter.JacksonConverter.fromBody(JacksonConverter.java:40)
              at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367)
              at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
              at java.lang.reflect.Proxy.invoke(Proxy.java:397)
              at $Proxy15.notifications(Unknown Source)
              at com.xxxxx.android.network.spice.NotificationSpiceRequest.loadDataFromNetwork(NotificationSpiceRequest.java:21)
              at com.xxxxx.android.network.spice.NotificationSpiceRequest.loadDataFromNetwork(NotificationSpiceRequest.java:9)
              at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:48)
              at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:150)
              at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:217)
              at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
              at java.lang.Thread.run(Thread.java:818)
       Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.xxxxx.android.model.NotificationResponse out of START_ARRAY token
              at [Source: retrofit.ExceptionCatchingTypedInput$ExceptionCatchingInputStream@8b4da58; line: 1, column: 2]
              at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:749)
              at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:745)
              at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1203)
              at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:147)
              at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:126)
              at com.fasterxml.jackson.databind.ObjectMapper._unwrapAndDeserialize(ObjectMapper.java:3126)
              at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3049)
              at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2206)
              at retrofit.converter.JacksonConverter.fromBody(JacksonConverter.java:36)
              at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367)
              at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
              at java.lang.reflect.Proxy.invoke(Proxy.java:397)
              at $Proxy15.notifications(Unknown Source)
              at com.xxxxx.android.network.spice.NotificationSpiceRequest.loadDataFromNetwork(NotificationSpiceRequest.java:21)
              at com.xxxxx.android.network.spice.NotificationSpiceRequest.loadDataFromNetwork(NotificationSpiceRequest.java:9)
              at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:48)
              at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:150)
              at com.octo.android.robospice.request.DefaultRequestRunner$1.run(DefaultRequestRunner.java:217)
              at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
              at java.lang.Thread.run(Thread.java:818)

提前致谢。如果您需要任何进一步的信息,请告诉我。

编辑:我忘了提到我使用以下内容:

ObjectMapper om = new ObjectMapper();
om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

1 个答案:

答案 0 :(得分:1)

您需要在NotificationResponse对象中使用@JsonCreator注释的构造函数,如下所示:

@JsonCreator
public NotificationResponse(Notification[] notifications) {
    this.notifications = notifications;
}