Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (3.5k points)

I need to convert a certain JSON string to a Java object. I am using Jackson for JSON handling. I have no control over the input JSON (I read from a web service). This is my input JSON:

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

Here is a simplified use case:

private void tryReading() {

    String jsonStr = "{\"wrapper\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}";

    ObjectMapper mapper = new ObjectMapper();  

    Wrapper wrapper = null;

    try {

        wrapper = mapper.readValue(jsonStr , Wrapper.class);

    } catch (Exception e) {

        e.printStackTrace();

    }

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

}

My entity class is:

public Class Student { 

    private String name;

    private String id;

    //getters & setters for name & id here

}

My Wrapper class is basically a container object to get my list of students:

public Class Wrapper {

    private List<Student> students;

    //getters & setters here

}

I keep getting this error and "wrapper" returns null. I am not sure what's missing. Can someone help please?

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

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

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

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

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

    .from(UnrecognizedPropertyException.java:53)

2 Answers

0 votes
by (46k points)

What is required is to adjust getter method, NOT field -- field is private (and not auto-detected); besides, getters have precedence over fields if both are visible. (There are methods to make private fields visible, too, but if you want to hold getter there's not much point)

So, getter should either be named getWrapper(), or annotated with:

@JsonProperty("wrapper")

or, you can also mark the POJO to ignore unknown properties:

@JsonIgnoreProperties(ignoreUnknown = true)

0 votes
by (1.7k points)

Problem : Your JSON has the wrapper field as ` ", but the Wrapper class would take a field called " students". So, that has to be corrected. In particular, the class would change as follows:

update your Wrapper Class

Alter your field in your Wrapper class to correspond to JSON field

Ensure the JSON string is correct:

String jsonStr = "{\"wrapper\": [{\"id\": "13", "name": "Fred"}]}}"; // Correct JSON

 

Updated `tryReading` Method

The updated `tryReading` method is as follows:

private void tryReading() {

String jsonStr = "{\"wrapper\": [{\"id\": "13", "name": "Fred"}]}}";

Object Mapper mapper = new ObjectMapper(); // Declare mapper as object

Wrapper wrapper = null;

// reading the json

try {

wrapper = mapper.readValue(jsonStr, Wrapper.class);

} catch (Exception e) {

e.printStackTrace();

}

// display

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

}

The names for fields within your Java class should, in fact, be an exact match for the same that are defined in JSON structure. In that situation, using the JSON format appropriately will prevent `UnrecognizedPropertyException`.

Related questions

0 votes
1 answer
0 votes
1 answer
asked Nov 12, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
0 votes
1 answer
asked Jul 29, 2019 in Java by Suresh (3.4k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...