反序列化动态JSON对象

时间:2019-03-17 14:03:28

标签: json gson

我有一个问题。我如何反序列化此JSON?:

{
"symbol": "AAPL",
"stock_exchange_short": "NASDAQ",
"timezone_name": "America/New_York",
"intraday": {
    "2019-03-15 15:30:00": {
        "open": "186.99",
        "close": "186.19",
        "high": "187.08",
        "low": "185.90",
        "volume": "10159747"
    },
    "2019-03-15 14:30:00": {
        "open": "187.04",
        "close": "187.00",
        "high": "187.32",
        "low": "186.89",
        "volume": "2642621"
    },
    "2019-03-15 13:30:00": {
        "open": "186.71",
        "close": "187.03",
        "high": "187.25",
        "low": "186.67",
        "volume": "2995048"
    },
    "2019-03-15 12:30:00": {
        "open": "186.69",
        "close": "186.72",
        "high": "186.81",
        "low": "186.38",
        "volume": "2762442"
    },
    "2019-03-15 11:30:00": {
        "open": "186.21",
        "close": "186.69",
        "high": "186.96",
        "low": "186.10",
        "volume": "3327219"
    },
    "2019-03-15 10:30:00": {
        "open": "185.25",
        "close": "186.22",
        "high": "186.49",
        "low": "184.91",
        "volume": "4503421"
    },
    "2019-03-15 09:30:00": {
        "open": "184.84",
        "close": "185.25",
        "high": "185.34",
        "low": "183.74",
        "volume": "12261774"
    }
}
}

我正在使用https://www.worldtradingdata.com中的API

我正在尝试制作Android App中的图表(x轴-日期和y轴-price)。 我必须如何反序列化此JSON以获取日期。日期不是静态的。

我使用了http://www.jsonschema2pojo.org

中的工具

它告诉我必须使用日期名称进行授课,但是日期更改会怎样?

在我以前的API中,我得到了这样的响应:

[
{
    "date": "2017-04-03",
    "open": 143.1192,
    "high": 143.5275,
    "low": 142.4619,
    "close": 143.1092,
    "volume": 19985714,
    "uOpen": 143.1192,
    "uHigh": 143.5275,
    "uLow": 142.4619,
    "uClose": 143.1092,
    "uVolume": 19985714,
    "change": 0.039835,
    "changePercent": 0.028,
    "label": "Apr 03, 17",
    "changeOverTime": -0.0039
},{  
    "date": "2017-04-2",
    "open": 143.1192,
    "high": 144.5275,
    "low": 142.4619,
    "close": 143.1092,
    "volume": 19985714,
    "uOpen": 143.1192,
    "uHigh": 143.5275,
    "uLow": 142.4619,
    "uClose": 143.1092,
    "uVolume": 19985714,
    "change": 0.039835,
    "changePercent": 0.028,
    "label": "Apr 03, 17",
    "changeOverTime": -0.0039
}]

我知道我该怎么做

我希望我把一切都解释好。

1 个答案:

答案 0 :(得分:0)

对于非静态属性名称,请使用Map。您的POJO模型如下所示:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import java.io.File;
import java.io.FileReader;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class GsonApp {

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

        Gson gson = new GsonBuilder().create();

        Stock response = gson.fromJson(json, Stock.class);

        response.getIntraday().forEach((k, v) -> System.out.println(k + " => " + v));
    }
}

class Stock {

    private String symbol;

    @SerializedName("stock_exchange_short")
    private String stockExchangeShort;

    @SerializedName("timezone_name")
    private String timeZone;

    private Map<String, Data> intraday;

    // getters, setters
}

class Data {
    private BigDecimal open;
    private BigDecimal close;
    private BigDecimal high;
    private BigDecimal low;
    private BigDecimal volume;

    // getters, setters
}

上面的代码显示:

2019-03-15 15:30:00 => Data{open=186.99, close=186.19, high=187.08, low=185.90, volume=10159747}
2019-03-15 14:30:00 => Data{open=187.04, close=187.00, high=187.32, low=186.89, volume=2642621}
2019-03-15 13:30:00 => Data{open=186.71, close=187.03, high=187.25, low=186.67, volume=2995048}
2019-03-15 12:30:00 => Data{open=186.69, close=186.72, high=186.81, low=186.38, volume=2762442}
2019-03-15 11:30:00 => Data{open=186.21, close=186.69, high=186.96, low=186.10, volume=3327219}
2019-03-15 10:30:00 => Data{open=185.25, close=186.22, high=186.49, low=184.91, volume=4503421}
2019-03-15 09:30:00 => Data{open=184.84, close=185.25, high=185.34, low=183.74, volume=12261774}