Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JSON数据映射到Java对象_Java_Json - Fatal编程技术网

将JSON数据映射到Java对象

将JSON数据映射到Java对象,java,json,Java,Json,我一直在尝试将JSON数据映射到Java对象,在我的PC上使用JSON文件,但它总是抛出异常: org.codehaus.jackson.map.exc.UnrecognizedPropertyException:未识别的字段“title”(类MovieResponse),未标记为可忽略 在[Source:C:\M.json;第2行,第13列](通过引用链:MovieResponse[“title”]) 我的数据类: import org.codehaus.jackson.annotate.Js

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

org.codehaus.jackson.map.exc.UnrecognizedPropertyException:未识别的字段“title”(类MovieResponse),未标记为可忽略
在[Source:C:\M.json;第2行,第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
            +"]";
  }
}
我的mapper类:

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库。

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

  • @JsonIgnoreProperties
    属性应该放在
    MovieResponse
    类之上,而不是
    Movie
    类之上。查看默认为false的“ignoreUnknown”属性,最值得注意的是:

    公共抽象布尔值忽略未知值

    属性,该属性定义是否可以忽略任何未识别的 反序列化期间的属性。如果为true,则所有 未被识别——也就是说,没有接受 它们将在没有警告的情况下被忽略(尽管处理程序未知) 属性(如果有的话)仍将被调用),没有例外

  • 您的setter不应该返回任何值,这可能解释了为什么Jackson没有看到“title”setter。以下是“标题”的设置方式:

    public void setTitle(String t) {
        title = t;
    }
    
  • >P>不是一个不工作的原因,但是你正在声明你的对象映射器两次,考虑使用你的映射器来代替一个新的映射器:

    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);
        }
    }
    
    这也行

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

    嗯,我得到了错误所在,并编辑了这个问题,现在代码运行没有任何错误/障碍,但没有显示任何输出。在读取值之后,考虑显示它。例如:System.out.println(“response”+response.toString());另外,如果它有效,请不要忘记向上投票并接受答案:-)我不知道为什么,但当我在类中编写System.out.println()时,它显示了一个错误,找不到符号,符号:在NetBeans中类输出