杰克逊映射对象或对象列表,具体取决于 json 输入
2022-09-03 05:14:11
我有这个POJO :
public class JsonObj {
private String id;
private List<Location> location;
public String getId() {
return id;
}
public List<Location> getLocation() {
return location;
}
@JsonSetter("location")
public void setLocation(){
List<Location> list = new ArrayList<Location>();
if(location instanceof Location){
list.add((Location) location);
location = list;
}
}
}
来自 json 输入的“location”对象可以是 Location 的简单实例,也可以是 Location 的数组。当它只是一个实例时,我得到这个错误:
Could not read JSON: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token
我尝试过实现自定义设置器,但它不起作用。如何根据 json 输入映射位置或列表?