0
我使用Jackson API在一組POJO類的幫助下將JSON文件解析爲對象。但是,當ObjectMapper獲取到@title場,以下UnrecognizedPropertyException錯誤:將JSON映射到POJO時發生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中聲明瞭該字段。有人suugested加入@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;
}
}
好吧,我會盡力的上述方案,我也注意到,如果我壓抑這個錯誤和運行,退出標題和方向是不會打印時,我打印出陣列的位置,除了標題和方向是空的任何其他值填充..任何想法,爲什麼這可能是考慮的價值aren'在Json fil中爲null è? – 2015-03-31 08:14:44
我想用「壓制這個錯誤」,你的意思是添加'@ JsonIgnore'註釋。基本上,你告訴傑克遜,如果它找不到那些領域,就不應該打擾他。由於沒有任何東西可以映射到它,它們將被分配'null'。除了需要指定正確的JSON鍵(即'@ title'而不是'title')之外,您使用的'@ JsonProperty'註釋是一種可行的方法。 – Maze 2015-03-31 12:28:05
謝謝,我會嘗試。 – 2015-03-31 12:40:02