2

我目前正在将 Alpha Vantage Api 实施到一个 react-native 应用程序中。我想要做的是获得每 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"
    }}}

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

4

1 回答 1

1

是的,您可以遍历时间序列对象并推入一个数组,如下所示:

var closingPrice = []; 
var timeSeriesData = responseJson['Time Series (15min)']; 
for(var key in timeSeriesData){
    closingPrice.push({timeStamp : key, closingPrice : timeSeriesData[key]['4. close']}
}
于 2018-05-03T09:37:35.127 回答