java json转抽象对象_将JSON数据映射到Java对象

我一直在尝试使用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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值