jimmy
jimmy

Reputation: 515

How to fix Error START_ARRAY token when deserializing Json with ModelMapper?

I'm trying to deserialize a JSON file in this format:

[
  ["AA", "GG", "1992/11/18"],
  ["BB", "DD", "2005/02/20"]
]

Using this class:

public class DataList {
    private List<String> att;
    // constructor, getter and setter
}

doing:

DataList [] dataList= mapper.readValue(ResourceUtils.getFile("classpath:" + filename), DataList [].class);

But I'm getting:

    com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `org.example.DataList ` out of START_ARRAY token
 at [Source: (File); line: 2, column: 3] (through reference chain: java.lang.Object[][0])

Any idea on how to fix this error?

Upvotes: 3

Views: 9853

Answers (1)

Andrei Yusupau
Andrei Yusupau

Reputation: 633

Jackson doesn't know how to map array of strings to DataList object. So you should just add @JsonCreate on DataList constructor to show Jackson what to use for the conversion.

public class DataList {

    private List<String> att;

    @JsonCreator
    public DataList(List<String> att) {
        this.att = att;
    }

    // constructor, getter and setter
}

Upvotes: 5

Related Questions