这是什么数据格式,我如何解析 Java 模型
What is this data format and how can I parse that do Java model
我正在使用外部 api (Binance) 来获取特定加密货币符号的价格,并且我收到以下格式的数据:
[
[
1614556800000,
"15.00700000",
"15.38700000",
"14.14100000",
"14.37900000",
"453842.30000000",
1615161599999,
"6709971.37110000",
3535,
"216741.30000000",
"3217265.31930000",
"0"
],
[
1615161600000,
"14.43300000",
"14.85900000",
"14.05000000",
"14.29200000",
"539248.70000000",
1615766399999,
"7792792.20870000",
3549,
"278787.20000000",
"4045258.39210000",
"0"
]
]
并且 api 对每个值的含义进行了解释。我尝试为这些字段创建 Java 模型并使用 RestTemplate 解析它们,但它不起作用。
PriceChange[] priceChangeArray = restTemplate.getForObject(uri, PriceChange[].class);
我期望在此 API 上使用 JSON 格式,导致其他端点 returns 值使用这种格式。我问这个问题是因为我想避免为此创建解析器。
编辑:
字段说明:
[
1499040000000, // Open time
"0.01634790", // Open
"0.80000000", // High
"0.01575800", // Low
"0.01577100", // Close
"148976.11427815", // Volume
1499644799999, // Close time
"2434.19055334", // Quote asset volume
308, // Number of trades
"1756.87402397", // Taker buy base asset volume
"28.46694368", // Taker buy quote asset volume
"17928899.62484339" // Ignore.
]
PriceChange.java
@Entity
@IdClass(PriceChangeKey.class)
public class PriceChange {
@Id
private String symbol;
private String interval;
@Id
@Temporal(TemporalType.TIMESTAMP)
private Date openTime;
@Id
@Temporal(TemporalType.TIMESTAMP)
private Date closeTime;
private Double open;
private Double high;
private Double low;
private Double close;
private Double volume;
private Double quoteAssetVolume;
private Integer numberOfTrades;
private Double takerBuyBaseAssetVolume;
private Double takerBuyQuoteAssetVolume;
private Double ignore;
}
格式 是 JSON,但布局不适合自动解析为 POJO。
解析为 Object[][]
,然后手动将条目映射到 POJO。
Object[][] data = restTemplate.getForObject(uri, Object[][].class);
List<PriceChange> priceChanges = new ArrayList<>();
for (Object[] record : data) {
priceChanges.add(new PriceChange(
((Number) record[0]).longValue(),
new BigDecimal((String) record[1]),
new BigDecimal((String) record[2]),
new BigDecimal((String) record[3]),
new BigDecimal((String) record[4]),
new BigDecimal((String) record[5]),
((Number) record[6]).longValue(),
new BigDecimal((String) record[7]),
((Number) record[8]).intValue(),
new BigDecimal((String) record[9]),
new BigDecimal((String) record[10]),
new BigDecimal((String) record[11])));
}
或者使用 setter 方法和任何合适的数据类型。
我正在使用外部 api (Binance) 来获取特定加密货币符号的价格,并且我收到以下格式的数据:
[
[
1614556800000,
"15.00700000",
"15.38700000",
"14.14100000",
"14.37900000",
"453842.30000000",
1615161599999,
"6709971.37110000",
3535,
"216741.30000000",
"3217265.31930000",
"0"
],
[
1615161600000,
"14.43300000",
"14.85900000",
"14.05000000",
"14.29200000",
"539248.70000000",
1615766399999,
"7792792.20870000",
3549,
"278787.20000000",
"4045258.39210000",
"0"
]
]
并且 api 对每个值的含义进行了解释。我尝试为这些字段创建 Java 模型并使用 RestTemplate 解析它们,但它不起作用。
PriceChange[] priceChangeArray = restTemplate.getForObject(uri, PriceChange[].class);
我期望在此 API 上使用 JSON 格式,导致其他端点 returns 值使用这种格式。我问这个问题是因为我想避免为此创建解析器。
编辑:
字段说明:
[
1499040000000, // Open time
"0.01634790", // Open
"0.80000000", // High
"0.01575800", // Low
"0.01577100", // Close
"148976.11427815", // Volume
1499644799999, // Close time
"2434.19055334", // Quote asset volume
308, // Number of trades
"1756.87402397", // Taker buy base asset volume
"28.46694368", // Taker buy quote asset volume
"17928899.62484339" // Ignore.
]
PriceChange.java
@Entity
@IdClass(PriceChangeKey.class)
public class PriceChange {
@Id
private String symbol;
private String interval;
@Id
@Temporal(TemporalType.TIMESTAMP)
private Date openTime;
@Id
@Temporal(TemporalType.TIMESTAMP)
private Date closeTime;
private Double open;
private Double high;
private Double low;
private Double close;
private Double volume;
private Double quoteAssetVolume;
private Integer numberOfTrades;
private Double takerBuyBaseAssetVolume;
private Double takerBuyQuoteAssetVolume;
private Double ignore;
}
格式 是 JSON,但布局不适合自动解析为 POJO。
解析为 Object[][]
,然后手动将条目映射到 POJO。
Object[][] data = restTemplate.getForObject(uri, Object[][].class);
List<PriceChange> priceChanges = new ArrayList<>();
for (Object[] record : data) {
priceChanges.add(new PriceChange(
((Number) record[0]).longValue(),
new BigDecimal((String) record[1]),
new BigDecimal((String) record[2]),
new BigDecimal((String) record[3]),
new BigDecimal((String) record[4]),
new BigDecimal((String) record[5]),
((Number) record[6]).longValue(),
new BigDecimal((String) record[7]),
((Number) record[8]).intValue(),
new BigDecimal((String) record[9]),
new BigDecimal((String) record[10]),
new BigDecimal((String) record[11])));
}
或者使用 setter 方法和任何合适的数据类型。