我正在与 Google Details API 合作
我使用 Jackson 作为解析器。
我有以下字符串:
{"close":{"day":0,"time":"2200"},"open":{"day":0,"time":"1500"}}
所以我使用 ObjectMapper:
ObjectMapper mapDetail = new ObjectMapper();
Timetab timetab = mapDetail.readValue(time.get(s), Timetab.class);
Timetab 类的位置:
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;}
}
我收到以下错误:
05:38:37,816 ERROR TaskUtils$LoggingErrorHandler:95 - Unexpected error occurred in scheduled task. org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "close"
有人可以帮助我吗?
谢谢
最佳答案
Jackson 将 JSON 属性映射到 JavaBean 属性。因此,JSON 属性 close
映射到类 Timetab
中的 bean 属性 close
,该属性不存在,因为您将 bean 属性命名为 cl
而不是 close
。
请注意,bean 属性的名称源自 getter 或 setter 的名称,而不是字段本身。因此,仍然有一个字段 cl 是可以的(但不建议):
private Close cl;
public Close getClose() { return cl; }
public void setClose(Close c) { this.cl = c; }
关于java - jackson 映射器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20031333/