您只需要在您的 POJO 中有 2 个从 JSON 映射的 setter。
public class TestPojo
{
String startDate;
String endDate;
String content;
//Default Constructor.
//Getter and Setter for startDate and endDate
public void setValue(String content)
{
this.content = content;
}
public void setData(String content)
{
this.content = content;
}
}
我的测试:
String s = "{\"startDate\": \"2013-05-10\",\"endDate\": \"2013-05-19\",\"value\": \"value1.5\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
TestPojo pojo = mapper.readValue(s, TestPojo.class);
System.out.println(pojo);
s = "{\"startDate\": \"2013-05-10\",\"endDate\": \"2013-05-19\",\"data\": \"data1.5\"}";
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
pojo = mapper.readValue(s, TestPojo.class);
System.out.println(pojo);
输出:
TestPojo [startDate=2013-05-10, endDate=2013-05-19, content=value1.5]
TestPojo [startDate=2013-05-10, endDate=2013-05-19, content=data1.5]