2

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"])
RedUno
  • 78
  • 1
  • 8
  • 1
    You could try to set `DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES` to false on the object mapper (`objectMapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false )`) – Thomas Oct 26 '16 at 12:02
  • thank you @Thomas but still the same error – RedUno Oct 26 '16 at 12:08
  • 1
    Try // jackson 1.9 and before objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); // or jackson 2.0 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); From: http://stackoverflow.com/questions/5455014/ignoring-new-fields-on-json-objects-using-jackson?rq=1 – Babasaheb Oct 26 '16 at 12:14
  • i'm using Jackson 2.5.3 'com.fasterxml.jackson.annotation.JsonIgnoreProperties' and 'objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOW‌​N_PROPERTIES, false)' but still have the same probleme – RedUno Oct 26 '16 at 12:21
  • thank you @Thomas , there was a probleme with android studio instant run ingoring the modifications i did ><, it work know :) – RedUno Oct 26 '16 at 12:26
  • thank you @Baba it works – RedUno Oct 26 '16 at 12:28

1 Answers1

1

The solution was to add this line

objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
RedUno
  • 78
  • 1
  • 8