将JSON数据映射到Java对象

时间:2013-04-18 18:25:58

标签: java json

我一直在尝试使用我的PC上的JSON文件将JSON数据映射到Java对象,但它总是抛出异常:

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "title" (Class MovieResponse), not marked as ignorable at [Source: C:\M.json; line: 2, column: 13] (through reference chain: 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.net.*;
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文件包含以下数据:

{
  "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."
}

我做错了什么?我正在使用杰克逊图书馆。

2 个答案:

答案 0 :(得分:4)

以下是我在代码中看到的问题列表:

  1. @JsonIgnoreProperties属性应该放在MovieResponse类之上,而不是Movie类。查看documentation,最明显的是关于“ignoreUnknown”属性的内容,默认为false:

      

    public abstract boolean ignoreUnknown

         

    定义是否可以忽略任何无法识别的属性的属性   反序列化期间的属性。如果为true,则表示所有属性   无法识别 - 也就是说,没有可以接受的制定者或创作者   他们 - 在没有警告的情况下被忽略(虽然处理程序是未知的   属性,如果有的话,仍将被调用),无一例外。

  2. 你的二传手不应该返回任何价值,这可以解释为什么杰克逊没有看到“头衔”的制定者。以下是“标题”的设定者应该如何:

    public void setTitle(String t) {
        title = t;
    }
    
  3. 不是它不起作用的原因,但是你要声明你的对象映射器两次,考虑使用你的映射器而不是实例化一个新映射器:

    MovieResponse response = mapper.readValue(new File("C:\\M.json"), MovieResponse.class);
    
  4. 编辑:我认为这是你的固定代码:

    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);
        }
    }
    

答案 1 :(得分:1)

这也会起作用

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
MovieResponse response = mapper.readValue(new File("C:\\M.json"), MovieResponse.class);