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"])
6
  • 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 )) Commented Oct 26, 2016 at 12:02
  • thank you @Thomas but still the same error Commented Oct 26, 2016 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: stackoverflow.com/questions/5455014/… Commented Oct 26, 2016 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 Commented Oct 26, 2016 at 12:21
  • thank you @Thomas , there was a probleme with android studio instant run ingoring the modifications i did ><, it work know :) Commented Oct 26, 2016 at 12:26

1 Answer 1

1

The solution was to add this line

objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.