我试图遍历API调用中的JSON并在我的react应用程序中绘制每个高交易价(最终目标是创建图表)。问题是,我需要事先知道确切的时间戳字符串,因为API的结构不是数字形式。 这是我的代码(请参阅控制台日志)。在创建循环之前,我目前正在测试适当的端点,这就是为什么看不到循环的原因。
如果您很好奇,this.state.stock是AMD,我正在对其进行测试。最终将是用户输入。 我应该如何循环?
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调用数据的示例
{
"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"
},
}
答案 0 :(得分:2)
如果您只是想遍历数据,请使用以下内容:
var strProp, strValue;
for (strProp in someArray) {
console.log(`Name: ${strProp}; Value: ${someArray[strProp}\n`;
}
这将为您显示名称。由于索引签名,您还可以执行以下操作以在代码中实际使用它们:
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"]
中读取内容。
答案 1 :(得分:0)
欢迎堆栈溢出。
JSON数据格式不是易于绘制图表的最佳格式,您可能需要首先通过迭代对象的日期/时间键将其转换为更合适的格式。它们是降序排列的,因此您还需要反转输入。
以下是一些示例代码:
const rawSeries = response.data['Time Series (5min)']
const series = Object.keys(rawSeries).reverse().map(timestamp => {
return {timestamp, value: rawSeries[timstamp]['2. high']}
})
// series will now contain an array of objects like this:
// [
// {timestamp: "2019-06-28 14:50:00", value: "30.5000"},
// {timestamp: "2019-06-28 14:55:00", value: "30.6100"},
// ...
//]
如果只需要一个没有标签的数据点数组,则可以执行以下操作:
const rawSeries = response.data['Time Series (5min)']
const series = Object.keys(rawSeries).reverse().map(timestamp => {
return rawSeries[timstamp]['2. high']
})
// series will now contain an array of values like this:
// ["30.5000","30.6100", ... ]
答案 2 :(得分:0)
Pl ease install the npm package of Alpha vantage Wrapper
var Alpha = require('alpha_vantage_api_wrapper').Alpha; //Alpha Wrapper
var alpha = new Alpha('demo');
var stock_Dates = []; //Array Of Data
var stock_opens = [];
var stock_highs = [];
var stock_lows = [];
var stock_closes = [];
var stock_volumes = [];
const intraDay = (data['Time Series (5min)']); //Time Series Data
// var intraDay_date = { intraday: stock_Dates };
for(var update in intraDay){
var stock_Date = update;
var stock_open = intraDay[update]['1. open'];//Narrowing the endpoints for accessing the req Data
var stock_high = intraDay[update]['2. high'];
var stock_low = intraDay[update]['3. low'];
var stock_close = intraDay[update]['4. close'];
var stock_volume = intraDay[update]['5. volume'];
stock_Dates.push(stock_Date); //Pushing the Data into the Array of Data
stock_opens.push(stock_open);
stock_highs.push(stock_high);
stock_lows.push(stock_low);
stock_closes.push(stock_close);
stock_volumes.push(stock_volume);
}