【问题标题】:UnrecognizedPropertyException when mapping JSON to POJO将 JSON 映射到 POJO 时出现 UnrecognizedPropertyException
【发布时间】: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


    【解决方案1】:

    您的注释不应该是@JsonProperty("@title"),因为这是 JSON 中键的名称吗? 另请注意,Jackson 有两个不同的命名空间,具体取决于您的版本。这很容易成为一个陷阱

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

    所以请确保您使用正确的注释

    【讨论】:

    • 好的,我会尝试上面的解决方案,我确实注意到,如果我抑制此错误并运行,当我将位置打印为数组时,退出标题和方向不会打印出来,每隔一个除了为 null 的 title 和 direction 之外,已填充值。知道为什么这可能会考虑 Json 文件中的值不为 null 吗?
    • 我猜“抑制此错误”是指添加@JsonIgnore 注释。基本上,您是在告诉杰克逊,如果找不到这些字段,它不应该打扰。由于没有任何东西可以映射到它,它们将被分配null。您使用的 @JsonProperty 注释是可行的方法,除了您需要指定正确的 JSON 密钥(即 @title 而不是 title)。
    • 谢谢,我试试看。
    • 现在我收到了Can not deserialize instance of java.lang.String[] out of VALUE_STRING token at [Source: C:\Users\Brian\Documents\Eclipse\Projects\AI_Project_Grail_Quest_3\bin\resources\game.json; line: 13, column: 13] (through reference chain: gmit.Location["location"]->Object[][0]->gmit.Location["exit"]->Object[][0]->gmit.Exit["@title"])
    • 猜我需要更改退出中的字段类型? @迷宫
    猜你喜欢
    • 2017-03-31
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多