我收到的JSON对象是这样的。使用jackson跳过JSON的第一级
{
"Question279":{
"ID":"1",
"Contents":"Some texts here",
"User":"John",
"Date":"2016-10-01"
}
我需要将JSON映射到以下java bean。
public class Question {
@JsonProperty("ID")
private String id;
@JsonProperty("Contents")
private String contents;
@JsonProperty("User")
private String user;
@JsonProperty("Date")
private LocalDate date;
//some getters and setters are skipped...
}
还要注意的是在上面的JSON对象Question279
第一水平不总是相同的,这取决于提供给获得JSON参数用户。我无法改变这种情况。
目前我正在使用这样的东西。
ObjectMapper mapper = new ObjectMapper();
String json = "{'Question279':{'ID':'1', 'Contents':'Some texts here', 'User':'John', 'Date':'2016-10-01'}"
Question question = mapper.readValue(json, Question.class);
但它不工作,当然,我得到了Question
类充满null
。如何使它在这种情况下工作?
你可以围绕Question pojo创建一个包装类。由于包装类将问题作为数据成员,所以可以将json字符串转换为包装类并检索内部对象 –