Search code examples
javajsondata-bindingjacksonpojo

UnrecognizedPropertyException when mapping JSON to POJO


I'm using the Jackson API to parse a JSON file to objects with the help of a set of POJO classes. But when the ObjectMapper gets to the @title field, the following UnrecognizedPropertyException error:

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"])

This is the JSON file being parsed in:

http://hastebin.com/qamacarumu.pl

I understand this is from the property not being recognized as the error states, but I'm not sure why as I've declared this field in the Exit POJO. Someone suugested adding @JsonProperty to the field like this, which didn't seem to work:

@JsonProperty("title")
private String[] title;

Does anyone have any idea how to resolve this error? Or can you offer an explanation of why this error is being thrown despite there being a title field present in the Exit POJO?

These are the POJO's:

Location:

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) + "]";
    }



}

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;
    }

}

Solution

  • Shouldn't your annotation be @JsonProperty("@title"), since that's the name of the key in the JSON? Also note, that Jackson comes with two different namespaces depending on your version. This can easily be a trap

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

    so make sure you use the correct annotation