[英]Parsing nested JSON Arrays using Jackson
Im using jacson to parse the following JSON array 我正在使用jacson解析以下JSON数组
[
{
"target": "something",
"datapoints": [
[
null,
1482223380
]]}]
Into this POJO 进入这个POJO
public class Response {
private String target;
private List<List<Double>> datapoints;
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public List<List<Double>> getData() {
return datapoints;
}
public void setData(List<List<Double>> data) {
this.datapoints = data;
}
}
Using the following code 使用以下代码
objectMapper.readValue(json, new TypeReference<List<Response>>() {});
This works partially, the outer list
and the target
is correct, however datapoints
is null. 这部分起作用, outer list
和target
是正确的,但是datapoints
为null。
My initial solution is taken from this answere. 我最初的解决方案是取自此的answere。
My question is, why are not datapoints
not parsed as expected? 我的问题是,为什么不按预期分析datapoints
? Do this has something todo with the null values inside the array? 这与数组内的空值有关吗?
You could write a custom JsonDeserializer
for the datapoints
field. 你可以写一个自定义JsonDeserializer
的datapoints
现场。
class MyDatapointsDeserializer extends JsonDeserializer<List<List<Double>>> {
private static final TypeReference<List<List<Double>>> TYPE_REF =
new TypeReference<List<List<Double>>>() {};
@Override
public List<List<Double>> deserialize(
JsonParser jp, DeserializationContext ctxt) throws IOException {
return jp.readValueAs(TYPE_REF);
}
}
Then annotate the field accordingly. 然后相应地注释该字段。
@JsonDeserialize(using = MyDatapointsDeserializer.class)
private List<List<Double>> datapoints;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.