2015-03-30 102 views
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; 
    } 

} 

回答

1

应该不是你的注释是@JsonProperty("@title"),因为这是在JSON的键的名称? 另请注意,根据您的版本,Jackson有两个不同的命名空间。这很容易成为陷阱

  • org.codehaus.jackson
  • com.fasterxml.jackson

所以一定要使用正确标注

+0

好吧,我会尽力的上述方案,我也注意到,如果我压抑这个错误和运行,退出标题和方向是不会打印时,我打印出阵列的位置,除了标题和方向是空的任何其他值填充..任何想法,为什么这可能是考虑的价值aren'在Json fil中为null è? – 2015-03-31 08:14:44

+0

我想用“压制这个错误”,你的意思是添加'@ JsonIgnore'注释。基本上,你告诉杰克逊,如果它找不到那些领域,就不应该打扰他。由于没有任何东西可以映射到它,它们将被分配'null'。除了需要指定正确的JSON键(即'@ title'而不是'title')之外,您使用的'@ JsonProperty'注释是一种可行的方法。 – Maze 2015-03-31 12:28:05

+0

谢谢,我会尝试。 – 2015-03-31 12:40:02