• Register
0 votes
161 views

Problem :

I am trying to convert the certain JSON string to the Java object. I am trying to use  Jackson for JSON handling. I have totally  no control over the input JSON I am reading it from a web service.

Below is my input JSON:

{"wrapper":[{"id":"09","name":"Fred"}]}

Below is my simplified use case:

private void tryToRead() {

String jsonString = "{\"wrapper\"\:[{\"id\":\"09\",\"name\":\"Fred\"}]}";

    ObjectMapper objmapper = new ObjectMapper(); 

    Wrapper wrapp = null;

    try {

        wrapp = mapper.readValue(jsonString , Wrapper.class);

    } catch (Exception exc) {

        exc.printStackTrace();

    }

    System.out.println("wrapper = " + wrapp);

}

Below is my entity class :

public Class MyStudent {

    private String name;

    private String id;

    //getters & setters for the  name & id here

}

Below is my MyWrapper class is basically the container object to get the list of students:

public Class MyWrapper {

    private List<MyStudent> mystudents;

    //getters & setters here

}

But I keep on getting below error and "wrapper" returns null.

Have anyone faced the same issue before?

org.codehaus.jackson.map.exc.UnrecognizedPropertyException:

Unrecognized field "wrapper" (Class MyWrapper), not marked as ignorable

at [Source: java.io.StringReader@1198891; line: 1, column: 23]

(through reference chain: MyWrapper["wrapper"])

at org.codehaus.jackson.map.exc.UnrecognizedPropertyException

from(UnrecognizedPropertyException.java:63)

7.5k points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

Please use the below code :

ObjectMapper myObjectMapper = getMyObjectMapper();
myObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

It will ignore all the properties that you have not declared.

38.6k points