如何从Alpha Vantage Intraday 5分钟间隔获得收盘价

时间:2018-03-20 20:30:39

标签: json react-native fetch alphavantage

我目前正在将Alpha Vantage Api应用到反应原生应用中。我想要做的是获得每15分钟时间段的收盘价。我正在考虑的可能是使用循环并将每个收盘价存储到数组中。但我对如何访问这些数据感到困惑。

这就是我目前为了获得当前价格和符号而做得非常好。

return fetch ('https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=15min&outputsize=full&apikey=demo')
        .then((response) => response.json())
        .then((responseJson) => {
            // console.log(responseJson);

            const lastRefreshed = responseJson['Meta Data']['3. Last Refreshed'];

            this.setState({
                tickerSymbol: responseJson['Meta Data']['2. Symbol'],
                stockPrice: responseJson['Time Series (15min)'][lastRefreshed]['4. close']
            }, function(){

            });

        })
        .catch((error)=>{
            console.error(error);
        });

这就是Json的回应。

{
"Meta Data": {
    "1. Information": "Intraday (15min) prices and volumes",
    "2. Symbol": "MSFT",
    "3. Last Refreshed": "2018-03-20 16:00:00",
    "4. Interval": "15min",
    "5. Output Size": "Full size",
    "6. Time Zone": "US/Eastern"
},
"Time Series (15min)": {
    "2018-03-20 16:00:00": {
        "1. open": "93.2650",
        "2. high": "93.3000",
        "3. low": "93.0900",
        "4. close": "93.1300",
        "5. volume": "3642086"
    },
    "2018-03-20 15:45:00": {
        "1. open": "93.5949",
        "2. high": "93.6200",
        "3. low": "93.2700",
        "4. close": "93.2700",
        "5. volume": "890793"
    },
    "2018-03-20 15:30:00": {
        "1. open": "93.5599",
        "2. high": "93.6500",
        "3. low": "93.4900",
        "4. close": "93.5900",
        "5. volume": "712366"
    },
    "2018-03-20 15:15:00": {
        "1. open": "93.4700",
        "2. high": "93.6390",
        "3. low": "93.4600",
        "4. close": "93.5550",
        "5. volume": "825406"
    },
    "2018-03-20 15:00:00": {
        "1. open": "93.4800",
        "2. high": "93.5350",
        "3. low": "93.3700",
        "4. close": "93.4700",
        "5. volume": "451393"
    },
    "2018-03-20 14:45:00": {
        "1. open": "93.5300",
        "2. high": "93.6000",
        "3. low": "93.4100",
        "4. close": "93.4900",
        "5. volume": "534200"
    }}}

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:1)

是的,您可以循环遍历时间序列对象并按如下方式推入数组:

var closingPrice = []; 
var timeSeriesData = responseJson['Time Series (15min)']; 
for(var key in timeSeriesData){
    closingPrice.push({timeStamp : key, closingPrice : timeSeriesData[key]['4. close']}
}