我正在编写一个使用robospice,改造和Jackson 2.4.X的Android应用,

这是我尝试反序列化的json,它是从rest服务返回的。返回的“数据”数组中的元素是我要反序列化的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);

最佳答案

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

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

关于java - 改进:使用一个元素反序列化数组时出错:“超出START_ARRAY token ”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29395611/

10-15 13:24