当前位置: 首页 > 知识库问答 >
问题:

Jackson JSONMappingException:无法从START_ARRAY标记反序列化类实例

方权
2023-03-14
{
  query: "rann of kutch",
  response: {
    image: [
      "http://www.ouralaskancruise.com/wp-content/uploads/2010/05/Princess_Denali_Express_Rail.jpg"
    ],
    source: "chatbot",
    text: "Very interesting.",
    time: 5572
  },
  uid: "11222"
}
String responseJson = null;
            try {
                responseJson = Utils.executeHttpGet(url);
            } catch (Exception e) {

                if (e != null) {
                    error = e.getMessage();
                    e.printStackTrace();
                }

            }
public class ResponseDetails {

    public String query;
    public String uid;
    public Response response;

}
public class Response {
    public Image image;
    public String source;
    public String text;
    public long time;

}
public class Image {
    public ArrayList<String> urls;
//public    String url;
}
11-07 18:48:34.321: W/System.err(15921): com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.Travel.TravelChat.DataClasses.Image out of START_ARRAY token
11-07 18:48:34.321: W/System.err(15921):  at [Source: java.io.StringReader@417fbc70; line: 4, column: 5] (through reference chain: com.Travel.TravelChat.DataClasses.ResponseDetails["response"]->com.Travel.TravelChat.DataClasses.Response["image"])
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1121)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:148)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:107)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:107)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
11-07 18:48:34.341: W/System.err(15921):    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
11-07 18:48:34.341: W/System.err(15921):    at com.Travel.TravelChat.Helpers.Utils.readResponseDetailsJson(Utils.java:97)
11-07 18:48:34.341: W/System.err(15921):    at com.Travel.TravelChat.Helpers.GetResponseTask.doInBackground(GetResponseTask.java:46)
11-07 18:48:34.341: W/System.err(15921):    at com.Travel.TravelChat.Helpers.GetResponseTask.doInBackground(GetResponseTask.java:1)
11-07 18:48:34.341: W/System.err(15921):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-07 18:48:34.341: W/System.err(15921):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-07 18:48:34.341: W/System.err(15921):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-07 18:48:34.341: W/System.err(15921):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
11-07 18:48:34.341: W/System.err(15921):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-07 18:48:34.341: W/System.err(15921):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-07 18:48:34.341: W/System.err(15921):    at java.lang.Thread.run(Thread.java:856)
11-07 18:48:34.341: W/dalvikvm(15921): threadid=11: thread exiting with uncaught exception (group=0x40c18a68)
11-07 18:48:34.351: E/AndroidRuntime(15921): FATAL EXCEPTION: AsyncTask #1

请帮忙。

谢了!

共有1个答案

罗奇文
2023-03-14

如果您希望保持对象模型,您的json应该如下所示:

{
    query: "rann of kutch",
    response: {
        image : {
            urls: ["http://www.ouralaskancruise.com/wp-content/uploads/2010/05/Princess_Denali_Express_Rail.jpg"]
        },
        source: "chatbot",
        text: "Very interesting.",
        time: 5572
    },
    uid: "11222"
}

编辑

正如@Chaosit所说的那样,JSON是不可更改的,将bean定义更改为

public class Response
{
    public List<String> image;
    public String source;
    public String text;
    public long time;
}
 类似资料: