【发布时间】:2014-08-08 01:22:46
【问题描述】:
我曾经成功解析过以下.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
}
]
]
我准备了以下 POJO 来将数据存储到:
public class GeoPoint {
public double latitude;
public double longitude;
}
...
public class ThreePoints {
public List<GeoPoint> points;
}
如何更改上面的 Jackson 解析器以便它可以处理嵌套数组? Jackson 能否将数据解析成ThreePoints.class 之类的嵌套类结构?
【问题讨论】:
标签: java json generics jackson multidimensional-array