嵌套Json对象数组的Java模型

时间:2019-03-04 15:55:56

标签: java android json alphavantage

我正在尝试找出如何创建模型类来解析来自Alpha Vantage api的Json数据,但是还无法对模型类的格式进行锻炼。

这是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"
    }
  }
}

或图形形式:

enter image description here

元数据显然是由字符串组成的对象:

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

但是,当我获得时间序列(每天)时,我会陷入困境。这些是我无法解决的问题:

  1. 对象“时间序列(每天)”,根据标题的不同,标题将变为“时间序列(每月)”或“时间序列(一天之内)”等。我需要为每个模型创建一个新模型吗?
  2. 进一步深入到“时间序列(每日)”中,您实际上拥有的是时间戳对象的数组列表,但是当我查找文档时,会说方括号表示一个对象。您将如何引用?
  3. 我当时在想引用时间对象的方法是获取“时间序列”(每日)对象,然后对其进行迭代以获取各个时间时间戳对象,但是我不知道如何为此创建模型,因为各自的时间戳不同。例如,模型将具有开盘价,最高价,最低价,收盘价,调整后的收盘价,交易量,股利金额,分割系数,但时间戳记在其中适合什么位置?

我希望这是有道理的,我试图使它尽可能清晰,但我理解如果我不能很好地解释它。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

如果您可以调整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)": [
    {
      "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"
    }
  ]
}

主要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);
}

}

元数据类


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

}

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

}

我使用this网站将JSON在线转换为POJO。很棒的工具。实时保护程序。我多希望我能帮忙。