如何循环通过Alpha Vantage时间戳记 [英] How to loop though Alpha Vantage timestamps
问题描述
我试图遍历API调用中的JSON并在我的react应用程序中绘制每个高交易价(最终目标将是创建图表).问题是,我需要事先知道确切的时间戳字符串,因为API的结构不是数字形式的. 这是我的代码(请参阅控制台日志).在创建循环之前,我目前正在测试适当的端点,这就是为什么看不到循环的原因.
I am trying to loop through the JSON from my API call and plot each high trade price in my react app (the end goal will be to create a chart). The issue is, I would need to know the exact timestamp string beforehand, because the API isn't structured numerically. Here is my code (see the console log). I am currently testing the proper endpoint BEFORE creating a loop, which is why you don't see one.
如果您很好奇,this.state.stock是AMD,我正在对其进行测试.最终将是用户输入. 我应该如何遍历?
If you are curious, the this.state.stock is AMD, which I am using to test it. Eventually it will be the user input. How am I supposed to loop through?
componentDidMount() {
axios
.get(`https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=${this.state.stock}&interval=5min&apikey=J6ED0QFWG3T1KLTD`)
.then((response) => {
this.setState({
dailyQuote: response.data
})
console.log("daily quote",this.state.dailyQuote['Time Series (5min)']['2019-06-27 14:15:00']['2. high'])
})
}
以下是API调用数据的示例
Here is a sample of the API call data
{
"Meta Data": {
"1. Information": "Intraday (5min) open, high, low, close prices and volume",
"2. Symbol": "amd",
"3. Last Refreshed": "2019-06-28 16:00:00",
"4. Interval": "5min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (5min)": {
"2019-06-28 16:00:00": {
"1. open": "30.3900",
"2. high": "30.4000",
"3. low": "30.3300",
"4. close": "30.3700",
"5. volume": "2242133"
},
"2019-06-28 15:55:00": {
"1. open": "30.3700",
"2. high": "30.4400",
"3. low": "30.3601",
"4. close": "30.3900",
"5. volume": "1294256"
},
"2019-06-28 15:50:00": {
"1. open": "30.4350",
"2. high": "30.4500",
"3. low": "30.3500",
"4. close": "30.3700",
"5. volume": "1265203"
},
"2019-06-28 15:45:00": {
"1. open": "30.4750",
"2. high": "30.4750",
"3. low": "30.4300",
"4. close": "30.4350",
"5. volume": "664693"
},
"2019-06-28 15:40:00": {
"1. open": "30.4850",
"2. high": "30.4900",
"3. low": "30.4550",
"4. close": "30.4700",
"5. volume": "539474"
},
"2019-06-28 15:35:00": {
"1. open": "30.4750",
"2. high": "30.5050",
"3. low": "30.4500",
"4. close": "30.4900",
"5. volume": "685410"
},
"2019-06-28 15:30:00": {
"1. open": "30.5100",
"2. high": "30.5200",
"3. low": "30.4600",
"4. close": "30.4800",
"5. volume": "376771"
},
"2019-06-28 15:25:00": {
"1. open": "30.5400",
"2. high": "30.5600",
"3. low": "30.5000",
"4. close": "30.5101",
"5. volume": "288554"
},
"2019-06-28 15:20:00": {
"1. open": "30.5600",
"2. high": "30.5600",
"3. low": "30.5200",
"4. close": "30.5350",
"5. volume": "218143"
},
"2019-06-28 15:15:00": {
"1. open": "30.5703",
"2. high": "30.5800",
"3. low": "30.5400",
"4. close": "30.5557",
"5. volume": "281558"
},
"2019-06-28 15:10:00": {
"1. open": "30.5700",
"2. high": "30.5850",
"3. low": "30.5500",
"4. close": "30.5750",
"5. volume": "290714"
},
"2019-06-28 15:05:00": {
"1. open": "30.5803",
"2. high": "30.6100",
"3. low": "30.5750",
"4. close": "30.5750",
"5. volume": "169868"
},
"2019-06-28 15:00:00": {
"1. open": "30.6050",
"2. high": "30.6100",
"3. low": "30.5800",
"4. close": "30.5850",
"5. volume": "186744"
},
"2019-06-28 14:55:00": {
"1. open": "30.5650",
"2. high": "30.6100",
"3. low": "30.5500",
"4. close": "30.6050",
"5. volume": "378489"
},
"2019-06-28 14:50:00": {
"1. open": "30.5700",
"2. high": "30.5800",
"3. low": "30.5500",
"4. close": "30.5650",
"5. volume": "247525"
},
}
推荐答案
如果您只是想遍历数据,请使用以下代码:
If you are just looking to loop through the data, use something like this:
var strProp, strValue;
for (strProp in someArray) {
console.log(`Name: ${strProp}; Value: ${someArray[strProp}\n`;
}
这将为您显示名称.由于索引签名,您还可以执行以下操作以在代码中实际使用它们:
That will show you the names. You can also do something like this to actually use them in code because of index signatures:
var data = { "meta" : "", "series" : ""};
for (var strProp in apiResults) {
if (strProp == "Meta Data") {
data["meta"] = apiResults[strProp];
} else if (strProp == "Time Series (5min)") {
data["series"] = apiResults[strProp];
} else {
console.error(`Cannot read data from API call! Returns: ${apiResults}`);
}
然后您可以用相同的方式从data["series"]
中读取.
You can then read from data["series"]
in the same way.
这篇关于如何循环通过Alpha Vantage时间戳记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!