我有以下 JSON...
{
"name":"MyThings",
"things":[
{
"num":"123"
},
{
"num":"456"
}
]
}
到目前为止,我有这么多的映射...
@JsonIgnoreProperties(ignoreUnknown = true)
public class ThingList {
private String name;
private int[] nums;
}
我如何使用杰克逊从对象数组映射到 int 数组?
您可以为此使用自定义反序列化程序:
class DeSerializer extends StdDeserializer<ThingList> {
protected DeSerializer() {
super(ThingList.class);
}
@Override
public ThingList deserialize(JsonParser p, DeserializationContext ctxt) {
// use p.getText() and p.nextToken to navigate through the xml and construct ThingList object
return something;
}
使用反序列化程序按如下方式初始化解析器:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule("configModule", Version.unknownVersion());
module.addDeserializer(ThingList.class, new DeSerializer());
mapper.registerModule(module);
ThingList tl = mapper.readValue(<json string>, ThingList.class);