Lorenzo Tassone
Lorenzo Tassone

Reputation: 383

Jackson mapper error

I'm working with the Google Details API

and I'm using Jackson as parser.

I have the following string:

{"close":{"day":0,"time":"2200"},"open":{"day":0,"time":"1500"}}

So I use a ObjectMapper:

ObjectMapper mapDetail = new ObjectMapper();
Timetab timetab = mapDetail.readValue(time.get(s), Timetab.class);

Where the Timetab class is:

public class Timetab {
public static class Close{
    private int day;
    private String time;
    public String getTime() {return time;}
    public void setTime(String time) {this.time = time;}
    public int getDay() {return day;}
    public void setDay(int day) {this.day = day;}
}
public static class Open{
    private int day;
    private String time;
    public int getDay() {return day;}
    public void setDay(int day) {this.day = day;}
    public String getTime() {return time;}
    public void setTime(String time) {this.time = time;}
}

private Close cl;
private Open op;
public Close getCl() {return cl;}
public void setCl(Close cl) {this.cl = cl;  }
public Open getOp() {return op;}
public void setOp(Open op) {this.op = op;}

}

And i got the following error:

05:38:37,816 ERROR TaskUtils$LoggingErrorHandler:95 - Unexpected error occurred in scheduled task. org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "close"

Someone can help me please?

Thank you

Upvotes: 0

Views: 182

Answers (1)

isnot2bad
isnot2bad

Reputation: 24444

Jackson maps JSON properties to JavaBean properties. So the JSON property close is mapped to a bean property close in class Timetab, which doesn't exist, because you named your bean property cl instead of close.

Note that the name of the bean property is derived from the name of the getter or setter, not the field itself. So its OK (but not suggested) to still have a field cl:

private Close cl;

public Close getClose() { return cl; }
public void setClose(Close c) { this.cl = c; }

Upvotes: 2

Related Questions