如何用杰克逊解析嵌套数组? [英] How to parse nested arrays with Jackson?
问题描述
我曾经成功地解析以下.json文件:
I used to successfully parse the following .json file:
[
{
"latitude": 49.419459253939316,
"longitude": 8.676411621072491
},
{
"latitude": 49.41946061080915,
"longitude": 8.676411644939083
},
{
"latitude": 49.420365910782735,
"longitude": 8.676438042403413
}
]
以下 Jackson脚本输出点的List
.
private static <T> List<T> parseFile(final String fileName,
Class<T> contentType) {
// ...
InputStream inputStream = // Open file
ObjectMapper objectMapper = new ObjectMapper();
try {
TypeFactory typeFactory = objectMapper.getTypeFactory();
CollectionType collectionType = typeFactory
.constructCollectionType(List.class, contentType);
return objectMapper.readValue(inputStream, collectionType);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
现在,数据集变得更加复杂.点的List
变为点的List
的List
.
我是用这种方式构造的-如果不正确,请纠正我.
Now the dataset gets more complicated. The List
of points becomes a List
of List
of points.
I structured it this way - please correct me if this is not correct.
[
[
{
"latitude": 49.419459253939316,
"longitude": 8.676411621072491
},
{
"latitude": 49.41946061080915,
"longitude": 8.676411644939083
},
{
"latitude": 49.420365910782735,
"longitude": 8.676438042403413
}
],
[
{
"latitude": 49.40460334213399,
"longitude": 8.670034018853409
},
{
"latitude": 49.404608057285145,
"longitude": 8.670028775634165
},
{
"latitude": 49.40506145685422,
"longitude": 8.66955817506422
}
]
]
我准备了以下POJO来将数据存储到:
I prepared the following POJOs to store the data into:
public class GeoPoint {
public double latitude;
public double longitude;
}
...
public class ThreePoints {
public List<GeoPoint> points;
}
如何更改上面的Jackson解析器,以便它可以处理嵌套数组?杰克逊能否将数据解析为嵌套的类结构,例如ThreePoints.class
?
How do I have to change the above Jackson parser so it can handle the nested arrays? Can Jackson parse the data into a nested class structure such as the ThreePoints.class
?
推荐答案
您可以编写一个简单的自定义反序列化器
You can write a simple custom deserializer
要将其反序列化到您的班级:
To deserialize it to your class:
public class GeoPoint {
public double latitude;
public double longitude;
}
public class ThreePoints {
public List<GeoPoint> points;
}
编写自定义反序列化器:
Write a custom deserializer:
class ThreePointsDeserializer extends StdDeserializer<ThreePoints> {
protected ThreePointsDeserializer() {
super(ThreePoints.class);
}
@Override
public ThreePoints deserialize(JsonParser parser, DeserializationContext ctx)
throws IOException, JsonProcessingException {
ThreePoints result = new ThreePoints();
GeoPoint[] points = parser.getCodec().readValue(parser, GeoPoint[].class);
result.points = Arrays.asList(points);
return result;
}
}
使用该反序列化器:
SimpleModule module = new SimpleModule();
module.addDeserializer(ThreePoints.class, new ThreePointsDeserializer());
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);
TypeFactory tf = mapper.getTypeFactory();
CollectionType collectionType = tf.constructCollectionType(List.class, ThreePoints.class);
List<ThreePoints> result = mapper.readValue(YOUR_DATA, collectionType);
这篇关于如何用杰克逊解析嵌套数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!