0

Unable to read value of image arr. How can I read the value of Image arr using jackson json mapper .

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"
}

Code:

String responseJson = null;
            try {
                responseJson = Utils.executeHttpGet(url);
            } catch (Exception e) {

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

            }

Classes

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;
}

Exception:

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

I have gone through the following links : JsonMappingException: out of START_ARRAY token

Cannot deserialize instance of object out of START_ARRAY token in Spring 3 REST Webservice

Please help .

Thanks!

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66

3 Answers3

1

If you want to keep you object model, your json should look like this :

{
    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"
}

edit

As the JSON can't be changed, like @Chaosit said, change your bean definition to

public class Response
{
    public List<String> image;
    public String source;
    public String text;
    public long time;
}

}

ToYonos
  • 16,469
  • 2
  • 54
  • 70
0

The problem is actually that you're trying to deserialize an instance of Image class when you have an array of strings. Try declaring a Response class in the following way:

public class Response {
@JsonProperty("image")
public List<String> images;
public String source;
public String text;
public long time; 

}

You can find these annotations here: https://github.com/FasterXML/jackson-annotations

Chaosit
  • 1,116
  • 7
  • 21
  • List images returns null value, changing it to image* gave the correct arr. thanks – Rachita Nanda Nov 07 '14 at 13:47
  • Oh yeah, sorry I've forgotten to add an annotation, please see the edited answer to discover the way to name variable in more convenient way – Chaosit Nov 07 '14 at 14:00
0

If , we send one object then no need JSON.stringifyIf else make an ajax call Like this to send group of objects.

data : "data=" + JSON.stringify(data)

then it works.

UmaShankar
  • 221
  • 5
  • 14