gpt4 book ai didi

java - 如何将异构 JSON 数组映射到 Java 对象?

转载 作者:行者123 更新时间:2023-12-04 15:08:44 25 4
gpt4 key购买 nike

我通过 API 下载了大量历史加密货币市场数据。它的格式如下:

[
[1601510400000,"4.15540000","4.16450000","4.15010000","4.15030000","4483.01000000",1601510459999,"18646.50051400",50,"2943.27000000","12241.83706500","0"],
...
[1609490340000,"4.94020000","4.95970000","4.93880000","4.94950000","5307.62000000",1609490399999,"26280.03711000",98,"3751.46000000","18574.22402400","0"]
]

我认为它是一个数组数组,内部数组包含异构类型(总是相同顺序的相同类型)。作为中间步骤,我已将它保存到文本文件中,但我想读回它并将其映射到这种类型的对象数组中:

    public class MinuteCandle {
private long openTime;
private double openValue;
private double highValue;
private double lowValue;
private double closeValue;
private double volume;
private long closeTime;
private double quoteAssetVolume;
private int numberOfTrades;
private double takerBuyBaseAssetVolume;
private double takerBuyQuoteAssetVolume;
private double someGarbageData;
//...
}

我正在使用 Spring 框架和 jackson 库进行 json 映射。这样做可行还是我应该以某种方式手动解析文本?

最佳答案

使用JsonFormat并在您将形状指定为 ARRAY 的地方用它注释您的类:

@JsonFormat(shape = JsonFormat.Shape.ARRAY)
class MinuteCandle

此外,如果您想存储价格,请考虑使用 BigDecimal 而不是 double

另见:

关于java - 如何将异构 JSON 数组映射到 Java 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65616684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com