0

I'm trying to parse the following JSON using Jackson, but only want the children of data returned as an array.

{
  "data": [
    {
      "lng": -1.3,
      "lat": 50.6,
      "address": "address"
    },
    {
      "lng": -0.3,
      "lat": 51.6,
      "address": "address"
    }
  ]
}

I've tried the following:

private Data[] readInData() {
        URL url = null;
        ObjectMapper objectMapper = new ObjectMapper();
        Data[] data = new Data[0];

        try {
            url = new URL(**url**);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            data = objectMapper.readValue(url, Data[].class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return data;
    }
public class Data {
    private String lng;
    private String lat;
    private String address;
}

But I get this exception:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `[Lcom.joshuaharwood.****.Data;` from Object value (token `JsonToken.START_OBJECT`)

Do I need a custom deserialiser? I'd really appreciate the help.

Many thanks.

0 Answers0