在我的Java
程序中,我试图解析从Strava.com的 API 获得的数据。我从那里收到的有效载荷之一JSON
如下所示:
[
{"type": "altitude","data": [519.1,519.3,519.3,519.4,519.5],"series_type": "distance","original_size": 5,"resolution": "high"},
{"type": "latlng","data": [[46.01234,6.01234],[46.11234,6.11234],[46.21234,6.21234],[46.31234,6.31234],[46.41234,6.41234]],"series_type": "distance","original_size": 5,"resolution": "high"},
{"type": "velocity_smooth","data": [0.0,0.0,0.0,5.5,5.2],"series_type": "distance","original_size": 5,"resolution": "high"},
{"type": "distance","data": [0.0,8.6,11.8,16.6,20.8],"series_type": "distance","original_size": 5,"resolution": "high"},
{"type": "time","data": [0,1,2,3,4],"series_type": "distance","original_size": 5,"resolution": "high"}
]
基本上,其中四个条目(高度、速度平滑、距离和时间)具有相同的结构(它们的data
字段是一个双精度数组(或可以解析为双精度的整数)),但第二个条目(latlng)有一点不同字段的结构data
(它是一个双精度数组)。
如果所有内容都已命名,我熟悉在和sJackson
之间转换的库,但不知道如何对上述数据结构进行建模以对其进行反序列化。JSON
POJO
假设不是上面的数据,而是如下所示:
{
"altitude": {"data": [519.1,519.3,519.3,519.4,519.5],"series_type": "distance","original_size": 5,"resolution": "high"},
"latlng": {"data": [[46.01234,6.01234],[46.11234,6.11234],[46.21234,6.21234],[46.31234,6.31234],[46.41234,6.41234]],"series_type": "distance","original_size": 5,"resolution": "high"},
"velocity_smooth": {"data": [0.0,0.0,0.0,5.5,5.2],"series_type": "distance","original_size": 5,"resolution": "high"},
"distance": {"data": [0.0,8.6,11.8,16.6,20.8],"series_type": "distance","original_size": 5,"resolution": "high"},
"time": {"data": [0,1,2,3,4],"series_type": "distance","original_size": 5,"resolution": "high"}
}
然后我可以定义以下三个类
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Value;
import java.util.List;
@Getter
@NoArgsConstructor
public class Holder {
DoubleData altitude;
CoordinateData latlng;
@JsonProperty("velocity_smooth") DoubleData velocitySmooth;
DoubleData distance;
DoubleData time;
}
@Getter
@NoArgsConstructor
public class DoubleData {
List<Double> data;
@JsonProperty("series_type") String seriesType;
@JsonProperty("original_size") Integer originalSize;
String resolution;
}
@Getter
@NoArgsConstructor
public class CoordinateData {
List<List<Double>> data;
@JsonProperty("series_type") String seriesType;
@JsonProperty("original_size") Integer originalSize;
String resolution;
}
然后使用
objectMapper.readValue(jsonString, Holder.class);
读入那个对象。但是,由于从 Strava 收到的数据是一个数组而不是一个对象,所以我失败了。我已经阅读了 Baeldung 关于如何解组到集合/数组的文章,但假设数组/集合中的所有类都是相同的。
我想定义一个接口,该接口将由可以在数组中找到的两个类扩展,然后使用该机制:
public interface Data {
}
@Getter
@NoArgsConstructor
public class DoubleData implements Data {
String type;
List<Double> data;
@JsonProperty("series_type") String seriesType;
@JsonProperty("original_size") Integer originalSize;
String resolution;
}
@Getter
@NoArgsConstructor
public class CoordinateData implements Data {
String type;
List<List<Double>> data;
@JsonProperty("series_type") String seriesType;
@JsonProperty("original_size") Integer originalSize;
String resolution;
}
Data[] array = objectMapper.readValue(jsonString, Data[].class);
但这不起作用,因为我需要找到某种方法让它找出何时使用DoubleData
类以及何时使用CoordinateData
类。
我敢肯定,我不是第一个尝试在Java
. 这可以做到吗?