我用于成功地解析以下上传.json文件:如何解析与杰克逊嵌套数组?
[
{
"latitude": 49.419459253939316,
"longitude": 8.676411621072491
},
{
"latitude": 49.41946061080915,
"longitude": 8.676411644939083
},
{
"latitude": 49.420365910782735,
"longitude": 8.676438042403413
}
]
以下Jackson script输出点的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
。 我这样构造它 - 如果这是不正确的请纠正我。
[
[
{
"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
}
]
]
我准备了以下的POJOs将数据存储到:
public class GeoPoint {
public double latitude;
public double longitude;
}
...
public class ThreePoints {
public List<GeoPoint> points;
}
怎么办我必须要改变上述杰克逊解析器,因此它可以处理嵌套数组?杰克逊可以将数据解析为一个嵌套的类结构,例如ThreePoints.class
?
你可以吗租赁更新您的解决方案,以显示如何生成一个'List'? –
JJD