gpt4 book ai didi

java - 改造:用一个元素反序列化数组时出错: "out of START_ARRAY token"

转载 作者:行者123 更新时间:2023-12-01 11:42:44 25 4
gpt4 key购买 nike

我正在编写一个使用 robospice、retrofit 和 Jackson 2.4.X 的 Android 应用程序,

这是我试图反序列化从休息服务返回的 json。返回的“data”数组中的元素是我想要反序列化的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 - 改造:用一个元素反序列化数组时出错: "out of START_ARRAY token",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29395611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com