axios 如何访问从API获取的数据

fd3cxomn  于 2023-08-04  发布在  iOS
关注(0)|答案(2)|浏览(128)

我想从this API获取股市数据并将其存储在变量中。我将需要一个循环,以获得每一天/条目的价格,但那是后来。股票数据数组在key:value对象的key部分有句子,如下所示:

"Time Series (Daily)": {
    "2022-05-06": {
        "1. open": "135.4700",
        "2. high": "137.9900",
        "3. low": "135.4700",
        "4. close": "137.6700",
        "5. volume": "7306396"
    },
}

字符串
如何将1. open的密钥锁定在Time Series (Daily)内部?
这就是我目前所拥有的一切:

var baseUrl = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=JX0BM5MCRRDMZBSE`;
let chartXValues = []; // Date
let chartYValues = []; //Stock Price

useEffect(() => {

    axios.get(baseUrl).then(res => {
        setChart(res);
        console.log(chart);
    });

}, [baseUrl]);

vfwfrxfs

vfwfrxfs1#

使用Object.keys(obj)可以获取未知密钥列表。使用这些键,您可以获取对象属性。
下面的代码片段可能会对您有所帮助。

import React, { useState, useEffect } from 'react';
import axios from 'axios';

function App(props) {

    var baseUrl = `https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=IBM&apikey=JX0BM5MCRRDMZBSE`;
    let chartXValues = []; // Date
    let chartYValues = []; //Stock Price

    const [chart, setChart] = useState();

    useEffect(() => {

        axios.get(baseUrl).then(res => {
            setChart(res.data);
            console.log(res.data);
        });

    }, [baseUrl]);

    useEffect(()=>{}, [chart]);

    return (
        <div>
            {   chart &&
                Object.keys(chart['Time Series (Daily)']).map((k, i1) => {
                    return (<div key={i1}>
                        <h3>{k}</h3>
                        {
                            Object.keys(chart['Time Series (Daily)'][k]).map((l, i2) => {
                                return (<div key={i2}>{chart['Time Series (Daily)'][k][l]}</div>)
                            })
                        }
                    </div>)
                })
            }
        </div>

    );
}

export default App;

字符串

jei2mxaa

jei2mxaa2#

您可以执行以下操作来提取所有“1”。从“时间序列(每日)”数据中打开“值”。

const response = {
    "Meta Data": {
        "1. Information": "Daily Prices (open, high, low, close) and Volumes",
        "2. Symbol": "IBM",
        "3. Last Refreshed": "2022-05-06",
        "4. Output Size": "Compact",
        "5. Time Zone": "US/Eastern"
    },
    "Time Series (Daily)": {
        "2022-05-06": {
            "1. open": "135.4700",
            "2. high": "137.9900",
            "3. low": "135.4700",
            "4. close": "137.6700",
            "5. volume": "7306396"
        },
        "2022-05-05": {
            "1. open": "136.4600",
            "2. high": "137.2600",
            "3. low": "134.7600",
            "4. close": "135.9200",
            "5. volume": "5957434"
        },
        "2022-05-04": {
            "1. open": "132.8700",
            "2. high": "137.8700",
            "3. low": "132.1400",
            "4. close": "137.4000",
            "5. volume": "5913705"
        },
    }
}

const openTs = Object.values(response["Time Series (Daily)"]).map(ts => ts["1. open"]);

字符串

相关问题