简体   繁体   中英

Convert JSON to Java class using GSON

I am trying to convert the following API data to a java class. I'm specifically interested in the picking out the timeSeriesDaily field with a date key and the closingStockPrice fields. I tried using the following data structures to capture the relevant fields

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;
    }

}

However I keep getting the following GSON error:

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

Would appreciate it anyone has answers as to why this isn't working and how to fix it.

Thanks.

PS: This is a sample of the JSON text.

 {
    "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"
        },

Make sure you json object is properly formatted and you is compatible with your POJO, Later you can use below code to do different type of conversion -

 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 -

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

Did you try going through the below similar question? May be you need to reorganize your class structure.

https://stackoverflow.com/questions/19169754/parsing-nested-json-data-using-gson

Your json structure is not right, however I have tried the following json with your classes and it's worked

{
    "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"
            }
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM