我一直在尝试使用我的PC上的JSON文件将JSON数据映射到Java对象,但它总是抛出异常:
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.UnrecognizedPropertyException:无法识别的字段title(类MovieResponse),未在[来源:C:\ M.json;上标记为可忽略的; line:2,column:13](通过引用链:MovieResponse [title])
我的数据类:
import org.codehaus.jackson.annotate.JsonProperty; public class MovieResponse{ private String title; private Integer year; @JsonProperty("mpaa_rating") private String mpaaRating; private Integer runtime; @JsonProperty("critics_consensus") private String criticsConsensus; public String getTitle(){ return title; } public String setTitle(String t){ return title = t; } public Integer getYear(){ return year; } public Integer setYear(Integer y){ return year = y; } public String getMpaa(){ return mpaaRating; } public String setMpaa(String mp){ return mpaaRating = mp; } public Integer getRuntime(){ return runtime; } public Integer setRuntime(Integer r){ return runtime = r; } public String getCritics(){ return criticsConsensus; } public String setCritics(String c){ return criticsConsensus = c; } @Override public String toString(){ return "MovieResponse[title="+title+",year="+year+",mpaa_Rating="+mpaaRating+",runtime="+runtime+",critics_Consensus="+criticsConsensus +"]"; } }我的映射器类:
import java.io.File; import java.io.IOException; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import java.*; import org.codehaus.jackson.annotate.JsonIgnoreProperties; @JsonIgnoreProperties public class Movie{ public static void main(String[] args) throws MalformedURLException, URISyntaxException, IOException { MovieResponse a = new MovieResponse(); ObjectMapper mapper = new ObjectMapper(); try{ MovieResponse response = new ObjectMapper().readValue(new File("C:\\M.json"), MovieResponse.class); }catch(MalformedURLException u){ u.printStackTrace(); } catch(IOException i){ i.printStackTrace(); } }json文件包含以下数据:
The json file contains following data:
{ "title": "Toy Story 3", "year": 2010, "mpaa_rating": "G", "runtime": 103, "critics_consensus": "Deftly blending comedy, adventure, and honest emotion, Toy Story 3 is a rare second sequel that really works." }我做错了什么?我正在使用Jackson库。
What am I doing wrong? I am using the Jackson library.
推荐答案以下是我在代码中看到的问题列表:
Here is a list of problems I see in your code:
@JsonIgnoreProperties 属性应该放在 MovieResponse上方 class,而不是 Movie 类。查看文档,最值得注意的是关于ignoreUnknown属性的说法,默认为false:
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
定义在反序列化期间是否可以忽略任何无法识别的属性的属性。如果为true,则无法识别无法识别的所有属性 - 即没有接受 的setter或创建者 - 会被忽略而不会发出警告(尽管处理程序为未知$ b) $ b属性(如果有的话)仍将被调用)无例外。
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.
你的setter不应该返回任何值,这个可以解释为什么杰克逊没有看到头衔的制定者。以下是标题的设置者应该如何:
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);
编辑:这是我的固定代码我认为:
here's your fixed code I think:
import java.io.File; import org.codehaus.jackson.map.ObjectMapper; import java.*; 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); } }更多推荐
将JSON数据映射到Java对象
发布评论