如何将异类json数组Map到java对象?

ie3xauqp  于 2021-06-27  发布在  Java
关注(0)|答案(3)|浏览(390)

我通过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"]
]

我将其视为一个数组,内部数组包含异构类型(总是以相同的顺序包含相同的类型)。作为中间步骤,我已将其保存到文本文件中,但我希望将其读回并Map到以下类型的对象数组:

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库进行jsonMap。这是可行的还是我应该手动解析文本?

3vpjnl9f

3vpjnl9f1#

我会分两步来做:
将json内容读入 List<Object> 和Jackson在一起。
转换每个 List<Object> 变成一个 MinuteCandle 对象并将这些对象收集到 MinuteCandle s。

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {

    public static void main(String[] args) throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        File file = new File("example.json");
        List<List<Object>> lists = objectMapper.readValue(file, new TypeReference<List<List<Object>>>() {});
        List<MinuteCandle> minuteCandles = new ArrayList<>();
        for (List<Object> list : lists) {
            minuteCandles.add(MinuteCandle.createFromList(list));
        }
    }
}

List<Object>MinuteCandle (上面的步骤2)可以通过在 MinuteCandle 班级。

public static MinuteCandle createFromList(List<Object> list) {
    MinuteCandle m = new MinuteCandle();
    m.openTime = (Long) list.get(0);
    m.openValue = Double.parseDouble((String) list.get(1));
    m.highValue = Double.parseDouble((String) list.get(2));
    m.lowValue = Double.parseDouble((String) list.get(3));
    m.closeValue = Double.parseDouble((String) list.get(4));
    m.volume = Double.parseDouble((String) list.get(5));
    m.closeTime = (Long) list.get(6);
    m.quoteAssetVolume = Double.parseDouble((String) list.get(7));
    m.numberOfTrades = (Integer) list.get(8);
    m.takerBuyBaseAssetVolume = Double.parseDouble((String) list.get(9));
    m.takerBuyQuoteAssetVolume = Double.parseDouble((String) list.get(10));
    m.someGarbageData = Double.parseDouble((String) list.get(11));
    return m;
}
jvidinwx

jvidinwx2#

假设文件中存储的文本是有效的json,类似于java中如何从txt文件读取json数据的解决方案?可以使用 com.google.gson.Gson 具体如下:

import com.google.gson.Gson;
import java.io.FileReader;
import java.io.Reader;

public class Main {
    public static void main(String[] args) throws Exception {
        try (Reader reader = new FileReader("somefile.txt")) {
            Gson gson = new Gson();
            MinuteCandle[] features = gson.fromJson(reader, MinuteCandle[].class);
        }
    }
}
wqsoz72f

wqsoz72f3#

使用jsonformat并在将shape指定为 ARRAY :

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

另外,考虑使用 BigDecimal 而不是 double 如果你想储存一个价格。
另请参见:
一个现实的例子,在货币中使用bigdecimal比使用double要好得多
如何用jackson反序列化混合类型的匿名数组

相关问题