Nicholas Joshua
Nicholas Joshua

Reputation: 13

Dynamic JSON value in POJO

Having trouble with dynamic JSON values and translating that into POJO classes. My retrofit2 response comes up with null so I assume my structuring is wrong.

My expected JSON output:

`

{
    "Meta Data": {
        "1. Information": "Intraday (5min) open, high, low, close prices and volume",
        "2. Symbol": "IBM",
        "3. Last Refreshed": "2022-10-26 19:40:00",
        "4. Interval": "5min",
        "5. Output Size": "Compact",
        "6. Time Zone": "US/Eastern"
    },
    "Time Series (5min)": {
        "2022-10-26 19:40:00": {
            "1. open": "135.0600",
            "2. high": "135.0700",
            "3. low": "135.0400",
            "4. close": "135.0700",
            "5. volume": "500"
        },
        "2022-10-26 19:05:00": {
            "1. open": "135.3500",
            "2. high": "135.3500",
            "3. low": "135.3500",
            "4. close": "135.3500",
            "5. volume": "106"
        },
        "2022-10-26 18:50:00": {
            "1. open": "135.0300",
            "2. high": "135.0300",
            "3. low": "135.0300",
            "4. close": "135.0300",
            "5. volume": "100"
        },
        "2022-10-26 17:50:00": {
            "1. open": "135.0300",
            "2. high": "135.0300",
            "3. low": "135.0300",
            "4. close": "135.0300",
            "5. volume": "200"
        }
    }
}

`

DailyQuote.java class

`

...



public class DailyQuote {

    @SerializedName("Meta Data")
    @Expose
    private MetaData metaData;
    @SerializedName("Time Series (5min)")
    @Expose
    private TimeSeries timeSeries;

    /**
     * No args constructor for use in serialization
     *
     */
    public DailyQuote() {
    }

    /**
     *
     * @param metaData
     * @param timeSeries
     */
    public DailyQuote(MetaData metaData, TimeSeries timeSeries) {
        super();
        this.metaData = metaData;
        this.timeSeries = timeSeries;
    }

    public MetaData getMetaData() {
        return metaData;
    }

    public void setMetaData(MetaData metaData) {
        this.metaData = metaData;
    }

    public TimeSeries getTimeSeries() {
        return timeSeries;
    }

    public void setTimeSeries(TimeSeries timeSeries) {
        this.timeSeries = timeSeries;
    }



}

`

MetaData.java class

`

...
public class MetaData {

    @SerializedName("1. Information")
    @Expose
    private String _1Information;
    @SerializedName("2. Symbol")
    @Expose
    private String _2Symbol;
    @SerializedName("3. Last Refreshed")
    @Expose
    private String _3LastRefreshed;
    @SerializedName("4. Interval")
    @Expose
    private String _4Interval;
    @SerializedName("5. Output Size")
    @Expose
    private String _5OutputSize;
    @SerializedName("6. Time Zone")
    @Expose
    private String _6TimeZone;

    /**
     * No args constructor for use in serialization
     *
     */
    public MetaData() {
    }

    /**
     *
     * @param _3LastRefreshed
     * @param _5OutputSize
     * @param _1Information
     * @param _4Interval
     * @param _6TimeZone
     * @param _2Symbol
     */
    public MetaData(String _1Information, String _2Symbol, String _3LastRefreshed, String _4Interval, String _5OutputSize, String _6TimeZone) {
        super();
        this._1Information = _1Information;
        this._2Symbol = _2Symbol;
        this._3LastRefreshed = _3LastRefreshed;
        this._4Interval = _4Interval;
        this._5OutputSize = _5OutputSize;
        this._6TimeZone = _6TimeZone;
    }

    public String get1Information() {
        return _1Information;
    }

    public void set1Information(String _1Information) {
        this._1Information = _1Information;
    }

    public String get2Symbol() {
        return _2Symbol;
    }

    public void set2Symbol(String _2Symbol) {
        this._2Symbol = _2Symbol;
    }

    public String get3LastRefreshed() {
        return _3LastRefreshed;
    }

    public void set3LastRefreshed(String _3LastRefreshed) {
        this._3LastRefreshed = _3LastRefreshed;
    }

    public String get4Interval() {
        return _4Interval;
    }

    public void set4Interval(String _4Interval) {
        this._4Interval = _4Interval;
    }

    public String get5OutputSize() {
        return _5OutputSize;
    }

    public void set5OutputSize(String _5OutputSize) {
        this._5OutputSize = _5OutputSize;
    }

    public String get6TimeZone() {
        return _6TimeZone;
    }

    public void set6TimeZone(String _6TimeZone) {
        this._6TimeZone = _6TimeZone;
    }

}

`

TimeSeries.java class

`

...
public class TimeSeries {
    @Expose
    private Map<String, date> dates;


    public TimeSeries(Map<String,date> dates){
        super();
        this.dates = dates;
    }
    public Map<String, date> getDates() {
        return dates;
    }
    public void setDates(Map<String,date> dates){
        this.dates = dates;
    }
}

`

And date.java class

`

public class date {

    @SerializedName("1. open")
    @Expose
    private String _1Open;
    @SerializedName("2. high")
    @Expose
    private String _2High;
    @SerializedName("3. low")
    @Expose
    private String _3Low;
    @SerializedName("4. close")
    @Expose
    private String _4Close;
    @SerializedName("5. volume")
    @Expose
    private String _5Volume;

    /**
     * No args constructor for use in serialization
     *
     */
    public date() {
    }

    /**
     *
     * @param _3Low
     * @param _1Open
     * @param _5Volume
     * @param _2High
     * @param _4Close
     */
    public date(String _1Open, String _2High, String _3Low, String _4Close, String _5Volume) {
        super();
        this._1Open = _1Open;
        this._2High = _2High;
        this._3Low = _3Low;
        this._4Close = _4Close;
        this._5Volume = _5Volume;
    }

    public String get1Open() {
        return _1Open;
    }

    public void set1Open(String _1Open) {
        this._1Open = _1Open;
    }

    public String get2High() {
        return _2High;
    }

    public void set2High(String _2High) {
        this._2High = _2High;
    }

    public String get3Low() {
        return _3Low;
    }

    public void set3Low(String _3Low) {
        this._3Low = _3Low;
    }

    public String get4Close() {
        return _4Close;
    }

    public void set4Close(String _4Close) {
        this._4Close = _4Close;
    }

    public String get5Volume() {
        return _5Volume;
    }

    public void set5Volume(String _5Volume) {
        this._5Volume = _5Volume;
    }

    @Override
    public String toString() {
        return "List:{" +
                "Open='" + get1Open() + '\'' +
                ", High='" + get2High() + '\'' +
                ", Low='" + get3Low() + '\'' +
                ", Close='" + get4Close() + '\'' +
                ", Volume='" + get5Volume();
    }

}

`

My retrofit2 enqueue responsebody for metadata works but when i look towards the TimeSeries it produces null results. I followed this: Gson.fromJson, how to use dynamic json value on my POJO class? , as it had similar JSON structuring of dynamic keys but to no avail. I am using GSON library and do not wish to use Jackson.

Thanks.

Upvotes: 1

Views: 84

Answers (1)

Digua
Digua

Reputation: 54

your json text "Time Series (5min)" don't has a dates child,and this json structure is so complex。you can modify your timeSeries like this:

   @SerializedName("Time Series (5min)")
   @Expose
   private Map<String, date> timeSeries

Upvotes: 1

Related Questions