2

在我的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之间转换的库,但不知道如何对上述数据结构进行建模以对其进行反序列化。JSONPOJO

假设不是上面的数据,而是如下所示:

{
  "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. 这可以做到吗?

4

1 回答 1

3

如果可能,您绝对应该使用他们的客户端。Strava API v3展示了许多如何将其API与他们的模型一起使用的示例。

如果你想实现你自己的模型,你应该考虑继承和com.fasterxml.jackson.annotation.JsonTypeInfo注释com.fasterxml.jackson.annotation.JsonSubTypes。此外,JSON Objectwith 类型包含以 . 形式latlng表示的对象列表。我们可以使用注解来处理这个问题。一起给出:JSONarraycom.fasterxml.jackson.annotation.JsonFormat

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.StringJoiner;

public class StravaApp {
    public static void main(String[] args) throws IOException {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.readValue(jsonFile, new TypeReference<List<Data>>() {}).forEach(System.out::println);
    }
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
        include = JsonTypeInfo.As.EXISTING_PROPERTY,
        visible = true,
        property = "type")
@JsonSubTypes({
        @JsonSubTypes.Type(name = "altitude", value = DoubleData.class),
        @JsonSubTypes.Type(name = "latlng", value = CoordinateData.class),
        @JsonSubTypes.Type(name = "velocity_smooth", value = DoubleData.class),
        @JsonSubTypes.Type(name = "distance", value = DoubleData.class),
        @JsonSubTypes.Type(name = "time", value = DoubleData.class)
})
abstract class Data<T> {

    private String type;

    @JsonProperty("series_type")
    private String seriesType;

    @JsonProperty("original_size")
    private Integer originalSize;

    private String resolution;

    private List<T> data;

    // getters, setters, toString
}


class DoubleData extends Data<Double> {

}

class CoordinateData extends Data<Coordinates> {

}

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
class Coordinates {
    private double lat;
    private double lng;

        // getters, setters, toString
}

上面的代码打印:

Data[type='altitude', seriesType='distance', originalSize=5, resolution='high', data=[519.1, 519.3, 519.3, 519.4, 519.5]]
Data[type='latlng', seriesType='distance', originalSize=5, resolution='high', data=[Coordinates[lat=46.01234, lng=6.01234], Coordinates[lat=46.11234, lng=6.11234], Coordinates[lat=46.21234, lng=6.21234], Coordinates[lat=46.31234, lng=6.31234], Coordinates[lat=46.41234, lng=6.41234]]]
Data[type='velocity_smooth', seriesType='distance', originalSize=5, resolution='high', data=[0.0, 0.0, 0.0, 5.5, 5.2]]
Data[type='distance', seriesType='distance', originalSize=5, resolution='high', data=[0.0, 8.6, 11.8, 16.6, 20.8]]
Data[type='time', seriesType='distance', originalSize=5, resolution='high', data=[0.0, 1.0, 2.0, 3.0, 4.0]]

您还应该查看Google Dev Group并查阅此解决方案。

于 2020-04-30T23:44:39.630 回答