无法读取图像arr的值。 如何使用jackson json mapper读取Image arr的值。
JSON
{
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
我已经浏览了以下链接: JsonMappingException: out of START_ARRAY token
Cannot deserialize instance of object out of START_ARRAY token in Spring 3 REST Webservice
请帮忙。
谢谢!
答案 0 :(得分:1)
如果你想保留对象模型,你的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"
}
修改强>
由于无法更改JSON,如@Chaosit所说,将bean定义更改为
public class Response
{
public List<String> image;
public String source;
public String text;
public long time;
}
}
答案 1 :(得分:0)
问题实际上是,当您有一个字符串数组时,您正在尝试反序列化Image类的实例。尝试按以下方式声明Response类:
public class Response {
@JsonProperty("image")
public List<String> images;
public String source;
public String text;
public long time;
}
您可以在此处找到这些注释:https://github.com/FasterXML/jackson-annotations
答案 2 :(得分:0)
如果,我们发送一个对象然后不需要JSON.stringify
如果做一个ajax调用就像这样发送一组对象。
data : "data=" + JSON.stringify(data)
然后它有效。