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`.