gpt4 book ai didi

java - 将 JSON 数据映射到 Java 对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:50:09 25 4
gpt4 key购买 nike

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

org.codehaus.jackson.map.exc.UnrecognizedPropertyException:无法识别的字段“title”(类 MovieResponse),未标记为可忽略
在 [来源: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
+"]";
}
}

我的映射器类:

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

最佳答案

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

  1. @JsonIgnoreProperties 属性应该放在 MovieResponse 类之上,而不是 Movie 类。查看documentation ,最值得注意的是关于“ignoreUnknown”属性的说法,默认为 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.

  2. 您的 setter 不应返回任何值,这可以解释为什么 Jackson 没有看到“title”setter。以下是“标题”的 setter 应该如何:

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

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

关于java - 将 JSON 数据映射到 Java 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16090464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com