【发布时间】:2015-06-04 04:05:59
【问题描述】:
我正在使用 Jackson API 在一组 POJO 类的帮助下将 JSON 文件解析为对象。但是当ObjectMapper到达@title字段时,出现如下UnrecognizedPropertyException错误:
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "@title" (class gmit.Exit), not marked as ignorable (2 known properties: "title", "direction"])
at [Source: C:\Users\Brian\Documents\Eclipse\Projects\AI_Project_Grail_Quest_3\bin\resources\game.json; line: 13, column: 24] (through reference chain: gmit.Location["location"]->Object[][0]->gmit.Location["exit"]->Object[][0]->gmit.Exit["@title"])
这是被解析的 JSON 文件:
http://hastebin.com/qamacarumu.pl
我知道这是由于属性未被识别为错误状态,但我不确定为什么,因为我已在 Exit POJO 中声明了此字段。有人建议像这样将@JsonProperty 添加到该字段中,这似乎不起作用:
@JsonProperty("title")
private String[] title;
有人知道如何解决这个错误吗?或者您能否解释一下为什么尽管 Exit POJO 中存在标题字段,但仍会引发此错误?
这些是 POJO:
地点:
import java.util.Arrays;
public class Location {
private Location[] location;
private String description;
private String name;
private Exit[] exit;
public Location[] getLocation() {
return location;
}
public void setLocation(Location[] location) {
this.location = location;
}
public String getDescription ()
{
return description;
}
public void setDescription (String description)
{
this.description = description;
}
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public Exit[] getExit() {
return exit;
}
public void setExit(Exit[] exit) {
this.exit = exit;
}
@Override
public String toString() {
return "Location [location=" + Arrays.toString(location)
+ ", description=" + description + ", name=" + name + ", exit="
+ Arrays.toString(exit) + "]";
}
}
退出:
public class Exit {
@JsonProperty("title")
private String[] title;
private String[] direction;
public String[] getTitle() {
return title;
}
public void setTitle(String[] title) {
this.title = title;
}
public String[] getDirection() {
return direction;
}
public void setDirection(String[] direction) {
this.direction = direction;
}
}
【问题讨论】:
标签: java json data-binding jackson pojo