1 回答
TA贡献1752条经验 获得超4个赞
只是一个想法。接受一个 Object 类型怎么样!!?然后您可以进行 instanceOf 检查并进行相应设置。
public class MyClass {
@JsonProperty("status")
Object status;
public boolean isStatus() {
return Boolean.valueOf(status.toString());
}
public void setStatus(Object status) {
if(status instanceof Boolean){
this.status = status;
}else if(status instanceof String){
this.status = status.toString().equals("free");
}
}
@Override
public String toString() {
return "MyClass [status=" + status.toString() + "]";
}
}
测试
String jsonString_1 = "{ \"status\":true}";
String jsonString_2 = "{ \"status\":\"free\"}";
ObjectMapper objectMapper = new ObjectMapper();
MyClass myObj_1 = objectMapper.readValue(jsonString_1.getBytes(), MyClass.class);
MyClass myObj_2 = objectMapper.readValue(jsonString_2.getBytes(), MyClass.class);
System.out.println(myObj_1);
System.out.println(myObj_2);
结果
MyClass [status=true]
MyClass [status=true]
添加回答
举报