Search code examples
javaandroidjsonalpha-vantage

Java model for arrays of nested Json objects


I am trying to work out how to create the model class/s for parsing Json data from Alpha Vantage api but have not been able to workout the format for the model class.

This is the format of the Json:

 {
  "Meta Data": {
    "1. Information": "Daily Time Series with Splits and Dividend Events",
    "2. Symbol": "FCHI",
    "3. Last Refreshed": "2015-08-21",
    "4. Output Size": "Full size",
    "5. Time Zone": "US/Eastern"
  },
  "Time Series (Daily)": {
    "2015-08-21": {
      "1. open": "47.4100",
      "2. high": "47.9100",
      "3. low": "47.4100",
      "4. close": "47.9100",
      "5. adjusted close": "47.9100",
      "6. volume": "5148",
      "7. dividend amount": "0.0000",
      "8. split coefficient": "1.0000"
    },
    "2015-08-20": {
      "1. open": "47.9000",
      "2. high": "47.9000",
      "3. low": "47.0600",
      "4. close": "47.2900",
      "5. adjusted close": "47.2900",
      "6. volume": "661",
      "7. dividend amount": "0.0000",
      "8. split coefficient": "1.0000"
    }
  }
}

or in graphical form:

enter image description here

Meta data is obviously an object made up of the strings:

String Information;
String Symbol;
String LastRefreshed;
String OutputSize;
String Time Zone;

But when I get Time Series (daily) I am stuck. These are the questions I haven't been able to work out:

  1. The object "Time Series (Daily)", depending on the function the title will change to Time Series (monthly), or Time Series (intra day) ect. Do i need to create a new model for each?
  2. Drilling further down into "Time Series (Daily)" you have what is essentially an arraylist of objects for the timestamps but when I look up the documentation is says brackets indicate an object. How would you reference this?
  3. I was thinking the way to reference the time objects would be to get the Time Series (Daily) object and iterate through it to get the individual time timestamp objects but I don't know how you would create the model for that because of the different timestamp for each. For example the model will have the open, high, low, close, adjusted close, volume, dividend amount, split coefficients but then where does the timestamp fit into that?

I hope that makes sense, I tried to make it as clear as possible but I understand if I didn't explain it very well.

Thanks for your help


Solution

  • If you could tweak your JSON response to match it to this below , you could achieve the feature you're trying with ease

    
       {
      "Meta Data": {
        "1. Information": "Daily Time Series with Splits and Dividend Events",
        "2. Symbol": "FCHI",
        "3. Last Refreshed": "2015-08-21",
        "4. Output Size": "Full size",
        "5. Time Zone": "US/Eastern"
      },
      "Time Series (Daily)": [
        {
          "1. open": "47.4100",
          "2. high": "47.9100",
          "3. low": "47.4100",
          "4. close": "47.9100",
          "5. adjusted close": "47.9100",
          "6. volume": "5148",
          "7. dividend amount": "0.0000",
          "8. split coefficient": "1.0000",
          "time_stamp": "2015-08-21"
        },
        {
          "1. open": "47.9000",
          "2. high": "47.9000",
          "3. low": "47.0600",
          "4. close": "47.2900",
          "5. adjusted close": "47.2900",
          "6. volume": "661",
          "7. dividend amount": "0.0000",
          "8. split coefficient": "1.0000",
          "time_stamp": "2015-08-20"
        }
      ]
    }
    
    

    The Main POJO

    package com.example;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class Example {
    
    private Meta_Data meta_Data;
    private List<Time_Series__Daily_> time_Series__Daily_ = null;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    
    public Meta_Data getMeta_Data() {
    return meta_Data;
    }
    
    public void setMeta_Data(Meta_Data meta_Data) {
    this.meta_Data = meta_Data;
    }
    
    public List<Time_Series__Daily_> getTime_Series__Daily_() {
    return time_Series__Daily_;
    }
    
    public void setTime_Series__Daily_(List<Time_Series__Daily_> time_Series__Daily_) {
    this.time_Series__Daily_ = time_Series__Daily_;
    }
    
    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }
    
    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
    
    }
    

    The Class MetaData

    
    package com.example;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class Meta_Data {
    
    private String _1__Information;
    private String _2__Symbol;
    private String _3__Last_Refreshed;
    private String _4__Output_Size;
    private String _5__Time_Zone;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    
    public String get1__Information() {
    return _1__Information;
    }
    
    public void set1__Information(String _1__Information) {
    this._1__Information = _1__Information;
    }
    
    public String get2__Symbol() {
    return _2__Symbol;
    }
    
    public void set2__Symbol(String _2__Symbol) {
    this._2__Symbol = _2__Symbol;
    }
    
    public String get3__Last_Refreshed() {
    return _3__Last_Refreshed;
    }
    
    public void set3__Last_Refreshed(String _3__Last_Refreshed) {
    this._3__Last_Refreshed = _3__Last_Refreshed;
    }
    
    public String get4__Output_Size() {
    return _4__Output_Size;
    }
    
    public void set4__Output_Size(String _4__Output_Size) {
    this._4__Output_Size = _4__Output_Size;
    }
    
    public String get5__Time_Zone() {
    return _5__Time_Zone;
    }
    
    public void set5__Time_Zone(String _5__Time_Zone) {
    this._5__Time_Zone = _5__Time_Zone;
    }
    
    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }
    
    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
    
    }
    
    

    The Class Time_Series_Daily

    
    package com.example;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class Time_Series__Daily_ {
    
    private String _1__open;
    private String _2__high;
    private String _3__low;
    private String _4__close;
    private String _5__adjusted_close;
    private String _6__volume;
    private String _7__dividend_amount;
    private String _8__split_coefficient;
    private String time_stamp;
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    
    public String get1__open() {
    return _1__open;
    }
    
    public void set1__open(String _1__open) {
    this._1__open = _1__open;
    }
    
    public String get2__high() {
    return _2__high;
    }
    
    public void set2__high(String _2__high) {
    this._2__high = _2__high;
    }
    
    public String get3__low() {
    return _3__low;
    }
    
    public void set3__low(String _3__low) {
    this._3__low = _3__low;
    }
    
    public String get4__close() {
    return _4__close;
    }
    
    public void set4__close(String _4__close) {
    this._4__close = _4__close;
    }
    
    public String get5__adjusted_close() {
    return _5__adjusted_close;
    }
    
    public void set5__adjusted_close(String _5__adjusted_close) {
    this._5__adjusted_close = _5__adjusted_close;
    }
    
    public String get6__volume() {
    return _6__volume;
    }
    
    public void set6__volume(String _6__volume) {
    this._6__volume = _6__volume;
    }
    
    public String get7__dividend_amount() {
    return _7__dividend_amount;
    }
    
    public void set7__dividend_amount(String _7__dividend_amount) {
    this._7__dividend_amount = _7__dividend_amount;
    }
    
    public String get8__split_coefficient() {
    return _8__split_coefficient;
    }
    
    public void set8__split_coefficient(String _8__split_coefficient) {
    this._8__split_coefficient = _8__split_coefficient;
    }
    
    public String getTime_stamp() {
    return time_stamp;
    }
    
    public void setTime_stamp(String time_stamp) {
    this.time_stamp = time_stamp;
    }
    
    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }
    
    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
    
    }
    
    

    I used this website to convert JSON to POJO online. Great tool. Real time saver. I wish i could help.