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

6 5 3
7,540 points

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.

9 7 4
38,600 points

Related questions

0 votes
1 answer 258 views
258 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 mphil 2.3k points
0 votes
1 answer 227 views
227 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 jwilliam 3.9k points
0 votes
1 answer 3.7K views
3.7K 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 49 views
49 views
Problem : According to the following documents: https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html I have the oauth user token and the user token secret, plus I have my app credentials... The docs state that the ... is supported by Twitter, but how should I include it in the get requests for screen names those are not publicly accessible?
asked Nov 25, 2019 alecxe 7.5k points
0 votes
1 answer 4 views
4 views
Hello all! I&rsquo;m newbie programmer. Currently I&rsquo;m working with jSON to fetch some data for my new project.But I&rsquo;ve stucked with some problems. I&rsquo;m trying using getJSON method to get value of it. Need help. Any help will be appreciated. What I want to get is jobDescription&rsquo;s value. {"JobData":{"JobDescription":"Cashier Wanted","JobSearch":"Search"}}
asked 22 hours ago mrain 120 points