通常情况下,我对使用
的数组内的对象做得很好models = mapper.readValue(jSONstream, mapper.getTypeFactory()
.constructCollectionType(List.class, Model.class));
但是这个HTTP请求会向我发回类似的内容。
Route.JSON
[
[
{
"length":1278.455,
"duration":660,
"legs":[
{
"length":342,
"duration":286.92,
"type":"walk",
"locs":[
{
"coord":{
"x":2548196,
"y":6678528
},
"arrTime":"201510271103",
"depTime":"201510271103",
"name":null
},
{
"coord":{
"x":2548221.7,
"y":6678553.5
},
"arrTime":"201510271103",
"depTime":"201510271103",
"name":"Purotie"
},
{
"coord":{
"x":2548380.4,
"y":6678731.4
},
"arrTime":"201510271107",
"depTime":"201510271107",
"name":null
},
{
"coord":{
"x":2548393.8,
"y":6678721.2
},
"arrTime":"201510271107",
"depTime":"201510271107",
"name":"Pit\u00e4j\u00e4nm\u00e4entie"
},
{
"coord":{
"x":2548426,
"y":6678692
},
"arrTime":"201510271108",
"depTime":"201510271108",
"name":"Takomotie",
"code":"1465104",
"shortCode":"1518",
"stopAddress":"Pit\u00e4j\u00e4nm\u00e4entie"
}
]
},
{
"length":456,
"duration":60,
"type":"5",
"code":"2550 2",
"locs":[
{
"coord":{
"x":2548426,
"y":6678692
},
"arrTime":"201510271108",
"depTime":"201510271108",
"name":"Takomotie",
"code":"1465104",
"shortCode":"1518",
"stopAddress":"Pit\u00e4j\u00e4nm\u00e4entie"
},
{
"coord":{
"x":2548768,
"y":6678460
},
"arrTime":"201510271109",
"depTime":"201510271109",
"name":"Valimotie",
"code":"1465102",
"shortCode":"1516",
"stopAddress":"Pit\u00e4j\u00e4nm\u00e4entie"
}
]
},
{
"length":479,
"duration":366.36,
"type":"walk",
"locs":[
{
"coord":{
"x":2548768,
"y":6678460
},
"arrTime":"201510271109",
"depTime":"201510271109",
"name":"Valimotie",
"code":"1465102",
"shortCode":"1516",
"stopAddress":"Pit\u00e4j\u00e4nm\u00e4entie"
},
{
"coord":{
"x":2548818.8,
"y":6678448
},
"arrTime":"201510271109",
"depTime":"201510271109",
"name":null
},
{
"coord":{
"x":2548822.7,
"y":6678455.2
},
"arrTime":"201510271110",
"depTime":"201510271110",
"name":"Pit\u00e4j\u00e4nm\u00e4entie"
},
{
"coord":{
"x":2548838.4,
"y":6678446.2
},
"arrTime":"201510271110",
"depTime":"201510271110",
"name":"H\u00f6yl\u00e4\u00e4m\u00f6tie"
},
{
"coord":{
"x":2548945.8,
"y":6678631.6
},
"arrTime":"201510271113",
"depTime":"201510271113",
"name":"Karvaamokuja"
},
{
"coord":{
"x":2549062,
"y":6678638
},
"arrTime":"201510271115",
"depTime":"201510271115",
"name":null
}
]
}
]
}
],
[
{
"length":1278.455,
"duration":660,
"legs":[
{
"length":342,
"duration":286.92,
"type":"walk",
"locs":[
{
"coord":{
"x":2548196,
"y":6678528
},
"arrTime":"201510271104",
"depTime":"201510271104",
"name":null
},
{
"coord":{
"x":2548221.7,
"y":6678553.5
},
"arrTime":"201510271104",
"depTime":"201510271104",
"name":"Purotie"
},
{
"coord":{
"x":2548380.4,
"y":6678731.4
},
"arrTime":"201510271108",
"depTime":"201510271108",
"name":null
},
{
"coord":{
"x":2548393.8,
"y":6678721.2
},
"arrTime":"201510271108",
"depTime":"201510271108",
"name":"Pit\u00e4j\u00e4nm\u00e4entie"
},
{
"coord":{
"x":2548426,
"y":6678692
},
"arrTime":"201510271109",
"depTime":"201510271109",
"name":"Takomotie",
"code":"1465104",
"shortCode":"1518",
"stopAddress":"Pit\u00e4j\u00e4nm\u00e4entie"
}
]
},
{
"length":456,
"duration":60,
"type":"5",
"code":"2270 2",
"locs":[
{
"coord":{
"x":2548426,
"y":6678692
},
"arrTime":"201510271109",
"depTime":"201510271109",
"name":"Takomotie",
"code":"1465104",
"shortCode":"1518",
"stopAddress":"Pit\u00e4j\u00e4nm\u00e4entie"
},
{
"coord":{
"x":2548768,
"y":6678460
},
"arrTime":"201510271110",
"depTime":"201510271110",
"name":"Valimotie",
"code":"1465102",
"shortCode":"1516",
"stopAddress":"Pit\u00e4j\u00e4nm\u00e4entie"
}
]
},
{
"length":479,
"duration":366.36,
"type":"walk",
"locs":[
{
"coord":{
"x":2548768,
"y":6678460
},
"arrTime":"201510271110",
"depTime":"201510271110",
"name":"Valimotie",
"code":"1465102",
"shortCode":"1516",
"stopAddress":"Pit\u00e4j\u00e4nm\u00e4entie"
},
{
"coord":{
"x":2548818.8,
"y":6678448
},
"arrTime":"201510271110",
"depTime":"201510271110",
"name":null
},
{
"coord":{
"x":2548822.7,
"y":6678455.2
},
"arrTime":"201510271111",
"depTime":"201510271111",
"name":"Pit\u00e4j\u00e4nm\u00e4entie"
},
{
"coord":{
"x":2548838.4,
"y":6678446.2
},
"arrTime":"201510271111",
"depTime":"201510271111",
"name":"H\u00f6yl\u00e4\u00e4m\u00f6tie"
},
{
"coord":{
"x":2548945.8,
"y":6678631.6
},
"arrTime":"201510271114",
"depTime":"201510271114",
"name":"Karvaamokuja"
},
{
"coord":{
"x":2549062,
"y":6678638
},
"arrTime":"201510271116",
"depTime":"201510271116",
"name":null
}
]
}
]
}
],
[
{
"length":1198.389,
"duration":960,
"legs":[
{
"length":1198,
"duration":998.34,
"type":"walk",
"locs":[
{
"coord":{
"x":2548196,
"y":6678528
},
"arrTime":"201510271105",
"depTime":"201510271105",
"name":null
},
{
"coord":{
"x":2548221.7,
"y":6678553.5
},
"arrTime":"201510271105",
"depTime":"201510271105",
"name":"Purotie - Takomotie"
},
{
"coord":{
"x":2548426,
"y":6678797.7
},
"arrTime":"201510271110",
"depTime":"201510271110",
"name":"Takomotie"
},
{
"coord":{
"x":2548455.9,
"y":6678803.9
},
"arrTime":"201510271110",
"depTime":"201510271110",
"name":"Kornetintie"
},
{
"coord":{
"x":2548760.5,
"y":6678595
},
"arrTime":"201510271116",
"depTime":"201510271116",
"name":"Valimotie"
},
{
"coord":{
"x":2548824.5,
"y":6678701.7
},
"arrTime":"201510271117",
"depTime":"201510271117",
"name":"Karvaamokuja"
},
{
"coord":{
"x":2549062,
"y":6678638
},
"arrTime":"201510271121",
"depTime":"201510271121",
"name":null
}
]
}
]
}
]
]
这是数组中的数组。我不知道如何正确解析它,我已经尝试了很多不同的方法来做到这一点。任何帮助表示赞赏。
P / S:这是所有getter和setter的模型。
Model.class
public class ModelRoutes {
List<ModelRoute> modelRoutes;
public class ModelRoute {
long lenght;
long duration;
List<Leg> legs;
public class Leg {
long lenght;
long duration;
String type;
String code;
List<Locs> locs;
List<Shape> shapes;
public class Locs {
Coord coord;
long arrTime;
long depTime;
String name;
long code;
String shortCode;
String stopAddress;
public class Coord {
double x;
double y;
}
}
public class Shape{
double x;
double y;
}
}
}
}
此外,对此运行单元测试会给出错误com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.avior.aviorandroid.Models.ModelRoutes out of START_ARRAY token at line: 1, column: 2
答案 0 :(得分:1)
您还需要一个clases才能在ModelRoute
和Leg
之间映射对象。
看看你的json的照片:
答案 1 :(得分:0)
DeanK
我将如下所示定义我的模型类,并使用组合来构造最外层的元素。
public class OuterMostClass {
private double length;
private int duration,
private InnerElement[] innerElements;
}
public class InnerElement {
private double length;
private double duration;
private String type;
private Location[] locs;
}
public class Location {
private Coordinates coords;
private Date arrTime;
private Date depTime;
private String name;
}
public class Coordinates {
private int x;
private int y;
}
请添加必要的getter和setter(使用任何IDE功能)并更正一些名称.Http请求的结果是OuterMostClass数组。所以你将得到一个OuterMostClass []数组。