简体   繁体   English

嵌套Json对象数组的Java模型

[英]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. 我正在尝试找出如何创建模型类来解析来自Alpha Vantage api的Json数据的方法,但还无法对模型类的格式进行锻炼。

This is the format of the 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"
    }
  }
}

or in graphical form: 或以图形形式:

在此处输入图片说明

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 谢谢你的帮助

If you could tweak your JSON response to match it to this below , you could achieve the feature you're trying with ease 如果您可以调整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"
    }
  ]
}

The Main POJO 主要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 班级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. 我使用网站在线将JSON转换为POJO。 Great tool. 很棒的工具。 Real time saver. 实时保护程序。 I wish i could help. 我多希望我能帮忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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