将 JSON 映射到 POJO 时发生 UnrecognizedPropertyException
UnrecognizedPropertyException when mapping JSON to POJO
我正在使用 Jackson API 在一组 POJO 类 的帮助下将 JSON 文件解析为 objects。但是当 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;
}
}
您的注释不应该是 @JsonProperty("@title")
,因为那是 JSON 中的键的名称?
另请注意,根据您的版本,Jackson 带有两个不同的名称空间。这很容易成为陷阱
org.codehaus.jackson
com.fasterxml.jackson
所以请确保使用正确的注释
我正在使用 Jackson API 在一组 POJO 类 的帮助下将 JSON 文件解析为 objects。但是当 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;
}
}
您的注释不应该是 @JsonProperty("@title")
,因为那是 JSON 中的键的名称?
另请注意,根据您的版本,Jackson 带有两个不同的名称空间。这很容易成为陷阱
org.codehaus.jackson
com.fasterxml.jackson
所以请确保使用正确的注释