I have been trying to map JSON data to Java objects, with the JSON file on my PC, but it always throws the exception:
org.codehaus.jackson.map.exc.Unrecognized
Here is a list of problems I see in your code:
The @JsonIgnoreProperties
attribute should be put above the MovieResponse
class, not the Movie
class. Check out the documentation, most notably what is said about the "ignoreUnknown" property, defaulted to false:
public abstract boolean ignoreUnknown
Property that defines whether it is ok to just ignore any unrecognized properties during deserialization. If true, all properties that are unrecognized -- that is, there are no setters or creators that accept them -- are ignored without warnings (although handlers for unknown properties, if any, will still be called) without exception.
Your setters should not return any value, this may explain why Jackson does not see a "title" setter. Here is how the setter for "title" should be:
public void setTitle(String t) {
title = t;
}
Not a reason for it to NOT work, but you are declaring your object mapper twice, consider using your mapper instead of instanciating a new one:
MovieResponse response = mapper.readValue(new File("C:\\M.json"), MovieResponse.class);
Edit: here's your fixed code I think:
import java.io.File;
import org.codehaus.jackson.map.ObjectMapper;
import java.net.*;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
public class Movie {
public static void main(String[] args) throws Exception {
MovieResponse response;
ObjectMapper mapper = new ObjectMapper();
response = mapper.readValue(new File("C:\\M.json"), MovieResponse.class);
System.out.println(response);
}
}
This will work too
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
MovieResponse response = mapper.readValue(new File("C:\\M.json"), MovieResponse.class);