Question

I am receiving data in a Jackson format and want to read it into a POJO I have defined. I am either badly defining the POJO or missing some key step. The data format being received is in the format below:

{
    streetSegment: [
    {
        distance: "0.04",
        highway: "residential",
        name: "Swift",
        line: "-1.6720224 52.6251985,-1.6721061 52.6250432,-1.6721799,             52.6248908,-1.6721996 52.6247594",
        wayId: "76473524"
    },
    {
        distance: "0.05",
        highway: "residential",
        name: "Swift",
        line: "-1.6721799 52.6248908,-1.6723374 52.6248669,-1.6732035 52.6249774,-1.6734643 52.6249894",
        wayId: "76473523"
    }
    ]
}

I have defined the POJO as:

public class StreetSegment {

public static class Street {
    private String distance;
    private String highway;
    private String name;
    private String line;
    private String wayId;
    public Street() {
        super();
    }
    public String getDistance() {
        return distance;
    }
    public void setDistance(String distance) {
        this.distance = distance;
    }
    public String getHighway() {
        return highway;
    }
    public void setHighway(String highway) {
        this.highway = highway;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLine() {
        return line;
    }
    public void setLine(String line) {
        this.line = line;
    }
    public String getWayId() {
        return wayId;
    }
    public void setWayId(String wayId) {
        this.wayId = wayId;
    }
}

}

When I read the object mapper with the following line:

List<StreetSegment> list = mObjectMapper.readValue( in, new TypeReference<List<StreetSegment>>(){} );

I get an exception as shown:

10-18 12:49:57.596: W/System.err(31776): com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
10-18 12:49:57.596: W/System.err(31776):  at [Source: java.io.StringReader@42ea8038; line: 1, column: 1]
10-18 12:49:57.606: W/System.err(31776):    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:575)
10-18 12:49:57.616: W/System.err(31776):    at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:569)
10-18 12:49:57.616: W/System.err(31776):    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:259)
10-18 12:49:57.626: W/System.err(31776):    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:217)
10-18 12:49:57.626: W/System.err(31776):    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:207)
10-18 12:49:57.636: W/System.err(31776):    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
10-18 12:49:57.636: W/System.err(31776):    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
10-18 12:49:57.646: W/System.err(31776):    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2041)
10-18 12:49:57.646: W/System.err(31776):    at co.uk.dominion.test.MainActivity$19._getStreetSegmentFromURL(MainActivity.java:3428)
10-18 12:49:57.646: W/System.err(31776):    at co.uk.dominion.test.MainActivity$19.doInBackground(MainActivity.java:3460)
10-18 12:49:57.656: W/System.err(31776):    at co.uk.dominion.test.MainActivity$19.doInBackground(MainActivity.java:1)
10-18 12:49:57.656: W/System.err(31776):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-18 12:49:57.656: W/System.err(31776):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-18 12:49:57.656: W/System.err(31776):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-18 12:49:57.656: W/System.err(31776):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-18 12:49:57.656: W/System.err(31776):    at java.lang.Thread.run(Thread.java:856)

I also tried defining as a JsonNode first to see if I could spot the issue but to no avail:

JsonNode node = mObjectReader.readValue(in);
List<StreetSegment> list = mObjectMapper.readValue(node.toString(), new TypeReference<List<StreetSegment>>(){});
Was it helpful?

Solution

Create Street by this way:

Street

public class Street {
    private List<StreetSegment> streetSegment;

    public Street() {
        // TODO Auto-generated constructor stub
    }

    public List<StreetSegment> getStreetSegment() {
        return streetSegment;
    }

    public void setStreetSegment(List<StreetSegment> streetSegment) {
        this.streetSegment = streetSegment;
    }
}

StreetSegment

public class StreetSegment {
    private String distance;
    private String highway;
    private String name;
    private String line;
    private String wayId;

    public StreetSegment() {

    }
    public String getDistance() {
        return distance;
    }
    public void setDistance(String distance) {
        this.distance = distance;
    }
    public String getHighway() {
        return highway;
    }
    public void setHighway(String highway) {
        this.highway = highway;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getLine() {
        return line;
    }
    public void setLine(String line) {
        this.line = line;
    }
    public String getWayId() {
        return wayId;
    }
    public void setWayId(String wayId) {
        this.wayId = wayId;
    }
}

To check it, here is main:

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        String str = "{" + 
                "    \"streetSegment\": [" + 
                "        {" + 
                "            \"distance\": \"0.04\"," + 
                "            \"highway\": \"residential\"," + 
                "            \"name\": \"Swift\"," + 
                "            \"line\": \"-1.6720224 52.6251985,-1.6721061 52.6250432,-1.6721799,             52.6248908,-1.6721996 52.6247594\"," + 
                "            \"wayId\": \"76473524\"" + 
                "        }," + 
                "        {" + 
                "            \"distance\": \"0.05\"," + 
                "            \"highway\": \"residential\"," + 
                "            \"name\": \"Swift\"," + 
                "            \"line\": \"-1.6721799 52.6248908,-1.6723374 52.6248669,-1.6732035 52.6249774,-1.6734643 52.6249894\"," + 
                "            \"wayId\": \"76473523\"" + 
                "        }" + 
                "    ]" + 
                "}";
    // to simulate stream reader    
    InputStreamReader in = new InputStreamReader(new ByteArrayInputStream(str.getBytes())); 

     BufferedReader streamReader = new BufferedReader(in); 

    StringBuilder buff = new StringBuilder();

    String inputStr;
    while ((inputStr = streamReader.readLine()) != null)
        buff.append(inputStr);


    ObjectMapper mapper = new ObjectMapper();

    Street mj = mapper.readValue(buff.toString(), Street.class);

        if(mj == null){
            System.err.println("null");
        }                
    }

Checking...

System.out.println(mj.getStreetSegment().get(0).getHighway());//dummy print

Output: residential

OTHER TIPS

Redefine your models in two seperate classes, viz StreetSegment and Street.

public class StreetSegment {
    list<Street> street_list;

    //getters and setters
}

and

public class Street {
     private String distance;
     private String highway;
     private String name;
     private String line;
     private String wayId;

      //getters and setters
 }

//skip the rest if your problem is solved

I'm guessing you're using @RequestBody for getting JSON body of request. I would use it this way:

public your_return_type your_method(@RequestBody String rb) {
    ObjectMapper om = new ObjectMapper();
    StreetSegment ss = om.readValue(rb, StreetSegment.class);

    //and the rest
    .
    .
    .
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top