• Register
0 votes
178 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

Related questions

0 votes
1 answer 5 views
5 views
Problem: Which controls perform binding by calling the databind method implicitly
asked Jul 3 asha 103k points
0 votes
1 answer 5 views
5 views
Problem: Is there a way to get around this : The specified http method is not allowed for the requested resource?
asked Jun 19 Florina Gulnar 94.5k points
0 votes
1 answer 9 views
9 views
Problem : Convert json to pojo in java using jackson
asked Jul 14 Siam55 90.4k points
0 votes
1 answer 25 views
25 views
Problem: To avoid exception, disable serializationfeature.fail_on_empty_beans.
asked Jul 3 Rada Gracelynn 91.6k points
0 votes
1 answer 15 views
15 views
Problem: So, what precisely is the problem here : No converter found for return value of type: class java.util.arraylist?
asked Jun 17 Nahil 107k points
0 votes
1 answer 157 views
157 views
Problem : I am trying to POST a List of custom objects but i am getting exception as below can not deserialize instance of java.util.arraylist out of start_object token
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 908 views
908 views
Problem : I am very new to Spring Boot. I am learning to write code in Spring Boot. I have written my first Spring Boot code as below @RequestMapping(value = "/bar/foo", method = RequestMethod.GET) public MyResponseEntity<foo> foo() { Foo model; ... return ... .core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.3</version> </dependency> How can I fix above Spring Boot exception?
asked Feb 29, 2020 mphil 2.3k points
0 votes
1 answer 490 views
490 views
Problem : I want to POST the List of custom objects. My JSON in a request body is as below : {     "collection": [         {             "name": "Test ordert1",             "detail": "ahk ksa"         },         {             "name": "Test ... } But I am facing below error: &ldquo;Can not deserialize instance of java.util.arraylist out of start_object token&rdquo; How can I fix the above error?
asked Jan 21, 2020 jwilliam 3.9k points
0 votes
1 answer 8.1k views
8.1k views
Problem : I am trying to POST a List of custom objects but i am getting exception as below can not deserialize instance of java.util.arraylist out of start_object token
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 1 view
1 view
Problem: Passing data from child to parent react.
asked 21 hours ago akhi 106k points