使用 GSON 将 JSON 转换为 Java 类

我正在尝试将以下 API 数据转换为 java 类。我特别感兴趣的是选择带有日期键的 timeSeriesDaily 字段和 openingStockPrice 字段。我尝试使用以下数据结构来捕获相关字段


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]

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


隔江千里
浏览 71回答 2
2回答

30秒到达战场

确保您的 json 对象格式正确并且与 POJO 兼容,稍后您可以使用下面的代码进行不同类型的转换 -&nbsp;Gson gson = new Gson();// 1. JSON file to Java objectStaff staff = gson.fromJson(new FileReader("C:\\projects\\test.json"), Test.class);// 2. JSON string to Java objectString json = "{'name' : 'test'}";Staff staff = gson.fromJson(json, Test.class);// 3. JSON file to JsonElement ->&nbsp; StringJsonElement json = gson.fromJson(new FileReader("C:\\projects\\test.json"), JsonElement.class);String result = gson.toJson(json);Maven 依赖 -<dependency>&nbsp; &nbsp; <groupId>com.google.code.gson</groupId>&nbsp; &nbsp; <artifactId>gson</artifactId>&nbsp; &nbsp; <version>x.x.x</version></dependency>

茅侃侃

你的 json 结构不正确,但是我已经在你的类中尝试了以下 json 并且它有效{&nbsp; &nbsp; "Meta Data": {&nbsp; &nbsp; &nbsp; &nbsp; "1. Information": "Daily Time Series with Splits and Dividend Events",&nbsp; &nbsp; &nbsp; &nbsp; "2. Symbol": "MSFT",&nbsp; &nbsp; &nbsp; &nbsp; "3. Last Refreshed": "2019-10-24",&nbsp; &nbsp; &nbsp; &nbsp; "4. Output Size": "Compact",&nbsp; &nbsp; &nbsp; &nbsp; "5. Time Zone": "US/Eastern"&nbsp; &nbsp; },&nbsp; &nbsp; "timeSeriesDaily": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2019-10-24": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "1. open": "139.3900",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2. high": "140.4100",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3. low": "138.6700",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "closingStockPrice": "139.9400",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5. adjusted close": "139.9400",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "6. volume": "34434281",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "7. dividend amount": "0.0000",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "8. split coefficient": "1.0000"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; ]}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java