使用 GSON 将 JSON 转换为 Java class

Convert JSON to Java class using GSON

我正在尝试将以下 API 数据转换为 java class。我对使用日期键和 closingStockPrice 字段挑选出 timeSeriesDaily 字段特别感兴趣。 我尝试使用以下数据结构来捕获相关字段

public class AlphavantageData {
    List<Map<String,TimeSeriesDaily>> timeSeriesDaily;

    public List<Map<String, TimeSeriesDaily>> getTimeSeriesDaily() {
        return timeSeriesDaily;
    }

    public void setTimeSeriesDaily(List<Map<String, TimeSeriesDaily>> timeSeriesDaily) {
        this.timeSeriesDaily = timeSeriesDaily;
    }


}

public class TimeSeriesDaily {

    private Map<String,DayCloseStockPrice> dayStockPriceRecords;

    public Map<String, DayCloseStockPrice> getDayStockPriceRecords() {
        return dayStockPriceRecords;
    }

    public void setDayStockPriceRecords(Map<String, DayCloseStockPrice> dayStockPriceRecords) {
        this.dayStockPriceRecords = dayStockPriceRecords;
    }

}

public class DayCloseStockPrice {
    private String closingStockPrice;

    public String getClosingStockPrice() {
        return closingStockPrice;
    }

    public void setClosingStockPrice(String closingStockPrice) {
        this.closingStockPrice = closingStockPrice;
    }

}

但是我不断收到以下 GSON 错误:

java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 10 column 10 path $.timeSeriesDaily[0]

如果有人能回答为什么这不起作用以及如何解决它,我们将不胜感激。

谢谢。

P.S.: 这是 JSON 文本的示例。

 {
    "Meta Data": {
        "1. Information": "Daily Time Series with Splits and Dividend Events",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2019-10-24",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "timeSeriesDaily": [
        "2019-10-24": {
            "1. open": "139.3900",
            "2. high": "140.4100",
            "3. low": "138.6700",
            "closingStockPrice": "139.9400",
            "5. adjusted close": "139.9400",
            "6. volume": "34434281",
            "7. dividend amount": "0.0000",
            "8. split coefficient": "1.0000"
        },

确保您的 json 对象格式正确并且与您的 POJO 兼容,稍后您可以使用下面的代码进行不同类型的转换 -

 Gson gson = new Gson();
// 1. JSON file to Java object
Staff staff = gson.fromJson(new FileReader("C:\projects\test.json"), Test.class);

// 2. JSON string to Java object
String json = "{'name' : 'test'}";
Staff staff = gson.fromJson(json, Test.class);

// 3. JSON file to JsonElement ->  String
JsonElement json = gson.fromJson(new FileReader("C:\projects\test.json"), JsonElement.class);
String result = gson.toJson(json);

Maven 依赖 -

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>x.x.x</version>
</dependency>

您的 json 结构不正确,但是我已经用您的 类 尝试了以下 json 并且有效

{
    "Meta Data": {
        "1. Information": "Daily Time Series with Splits and Dividend Events",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2019-10-24",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "timeSeriesDaily": [
        {
            "2019-10-24": {
                "1. open": "139.3900",
                "2. high": "140.4100",
                "3. low": "138.6700",
                "closingStockPrice": "139.9400",
                "5. adjusted close": "139.9400",
                "6. volume": "34434281",
                "7. dividend amount": "0.0000",
                "8. split coefficient": "1.0000"
            }
        }
    ]
}