亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 GSON 將 JSON 轉換為 Java 類

使用 GSON 將 JSON 轉換為 Java 類

隔江千里 2023-12-30 15:31:47
我正在嘗試將以下 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]如果有人能回答為什么這不起作用以及如何解決它,我將不勝感激。
查看完整描述

2 回答

?
30秒到達戰場

TA貢獻1828條經驗 獲得超6個贊

確保您的 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>


查看完整回答
反對 回復 2023-12-30
?
茅侃侃

TA貢獻1842條經驗 獲得超21個贊

你的 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"

            }

        }

    ]

}


查看完整回答
反對 回復 2023-12-30
  • 2 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號