RedUno
RedUno

Reputation: 78

Ignore a nested json field when deserializing object

I'm trying to deserialize this JSON string using JACKSON,

   [
    {
        "name": "United Kingdom",
        "woeid": 23424975,
        "placeType": {
                        "name": "Country",
                        "code": 12
                    }
     }
   ]

my class definition is

@JsonIgnoreProperties(ignoreUnknown = true)
public class Woeid {
    private String name;
    private Long woeid;

    public Woeid() {
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Long getWoeid() {
        return woeid;
    }
    public void setWoeid(Long woeid) {
        this.woeid = woeid;
    }
    @Override
    public String toString() {
        return name;
    }
}

and i use this code for deserialization

public List<Woeid> parse(String json) throws IOException {
    jp = jsonFactory.createParser(json);
    Woeid[] woeids= objectMapper.readValue(jp, Woeid[].class);
    return Arrays.asList(woeids);
}

but this error keeps comming, it work only if i remove "placeType" from the json string

com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
 at [Source: [{"name": "United Kingdom","woeid": 23424975,"placeType": {"name": "Country","code": 12}}]; line: 1, column: 45] 
(through reference chain: [Ljava.lang.Object[][0]->com.one.red.hashtagsdictionnary.model.Woeid["placeType"])

Upvotes: 2

Views: 2870

Answers (1)

RedUno
RedUno

Reputation: 78

The solution was to add this line

objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Upvotes: 1

Related Questions