将JSON数据映射到Java对象

发布于 2021-01-30 15:48:24

我一直在尝试使用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."
}

我究竟做错了什么?我正在使用Jackson图书馆。

关注者
0
被浏览
109
1 个回答
  • 面试哥
    面试哥 2021-01-30
    为面试而生,有面试问题,就找面试哥。

    这是我在您的代码中看到的问题列表:

    1. @JsonIgnoreProperties属性应放在MovieResponse类之上,而不是Movie类之上。查阅文档,最值得注意的是有关“ ignoreUnknown”属性的说法,默认为false:

    公共抽象布尔值 ignoreUnknown

    定义可以在反序列化期间忽略任何无法识别的属性的属性。如果为true,则所有无法识别的属性(即 没有设置器或创建者接受它们
    )都将在没有警告的情况下被忽略(尽管仍会调用未知属性的处理程序)。

    1. 您的设置员不应返回任何值,这可以解释为什么杰克逊没有看到“标题”设置员。这是“ title”的设置方法:

      public void setTitle(String t) {
      title = t;
      

      }

    2. 这不是使其不起作用的原因,但是您要两次声明对象映射器,请考虑使用映射器,而不是实例化一个新的映射器:

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

    编辑:这是我认为的固定代码:

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


推荐阅读
知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看