使用动态属性名称访问嵌套属性

4

我有一个从这个JSON生成的对象:

{
    "data":  {
        "Meta Data": {
            "1. Information": "Monthly Prices (open, high, low, close) and Volumes",
            "2. Symbol": "MSFT",
            "3. Last Refreshed": "2017-08-18",
            "4. Time Zone": "US/Eastern"
        },
        "Monthly Time Series": {
            "2017-08-18": {
                "1. open": "73.1000",
                "2. high": "74.1000",
                "3. low": "71.2800",
                "4. close": "72.4900",
                "5. volume": "285933387"
            },
            "2017-07-31": {
                "1. open": "69.3300",
                "2. high": "74.4200",
                "3. low": "68.0200",
                "4. close": "72.7000",
                "5. volume": "451248934"
            }
        }
    }
}

使用Object.keys(),我能够获取到键"Monthly time series",但是如何访问它内部的键呢?我想在"Monthly time series"键中运行一个for循环,遍历其中的键。我该怎么做呢?

在执行此操作后,@Hitmands 中的 Json 出现意外的 'o' 标记。 - Aayushi
你有一个JSON或JavaScript对象吗? - Hitmands
1
你为什么要使用 Object.keys() 来访问一个非随机属性,比如 "Monthly Time Series" - Andreas
你还有什么建议吗?@Andreas - Aayushi
2
var monthlyTimeSeries = object["data"]["Monthly Time Series"]; - Andreas
显示剩余3条评论
2个回答

2
您可以使用Object.keys()来获取键的数组,但我建议您使用Array.prototype.forEach()迭代键的数组,而不是使用for...loop。

代码:

const obj = {"data": {"Meta Data": {"1. Information": "Monthly Prices (open, high, low, close) and Volumes","2. Symbol": "MSFT","3. Last Refreshed": "2017-08-18","4. Time Zone": "US/Eastern"},"Monthly Time Series": {"2017-08-18": {"1. open": "73.1000","2. high": "74.1000","3. low": "71.2800","4. close": "72.4900","5. volume": "285933387"},"2017-07-31": {"1. open": "69.3300","2. high": "74.4200","3. low": "68.0200","4. close": "72.7000","5. volume": "451248934"}}}};

Object
  .keys(obj.data['Monthly Time Series'])
  .forEach(function (k) {
    console.log(obj.data['Monthly Time Series'][k]['1. open']);
  });


1
如何访问“月度时间序列”中的“1.open”值?如果我重复上述方法,它会显示未定义! - Aayushi

1

你可以通过Object.keys函数来访问键,就像你现在正在访问它们一样。

var data = JSON.parse('{"data": {\
    "Meta Data": {\
        "1. Information": "Monthly Prices (open, high, low, close) and Volumes",\
        "2. Symbol": "MSFT",\
        "3. Last Refreshed": "2017-08-18",\
        "4. Time Zone": "US/Eastern"\
    },\
    "Monthly Time Series": {\
        "2017-08-18": {\
            "1. open": "73.1000",\
            "2. high": "74.1000",\
            "3. low": "71.2800",\
            "4. close": "72.4900",\
            "5. volume": "285933387"\
        },\
        "2017-07-31": {\
            "1. open": "69.3300",\
            "2. high": "74.4200",\
            "3. low": "68.0200",\
            "4. close": "72.7000",\
            "5. volume": "451248934"\
        }\
}}}');

console.log(Object.keys(data['data']["Monthly Time Series"]));

输出:

(2) ["2017-08-18", "2017-07-31"]

啊,经过您的编辑,如果您想循环遍历它们,您不需要使用 Object.keys 函数 - 您可以像这样简单地遍历对象:

for (var i in data['data']['Monthly Time Series'])
    console.log(i, data['data']['Monthly Time Series'][i])

i 将包含您的密钥


根据评论:
var obj = {"data": {
    "Meta Data": {
        "1. Information": "Monthly Prices (open, high, low, close) and Volumes",
        "2. Symbol": "MSFT",
        "3. Last Refreshed": "2017-08-18",
        "4. Time Zone": "US/Eastern"
    },
    "Monthly Time Series": {
        "2017-08-18": {
            "1. open": "73.1000",
            "2. high": "74.1000",
            "3. low": "71.2800",
            "4. close": "72.4900",
            "5. volume": "285933387"
        },
        "2017-07-31": {
            "1. open": "69.3300",
            "2. high": "74.4200",
            "3. low": "68.0200",
            "4. close": "72.7000",
            "5. volume": "451248934"
        }
}}}

Object.keys(obj.data["Monthly Time Series"]).map(function(key, index){ return obj.data["Monthly Time Series"][key]["1. open"]; });

从数组中获取一个值: ```html

要从数组中获取一个值:

```
(2) ["73.1000", "69.3300"]

或者:

Object.keys(obj.data["Monthly Time Series"]).map(function(key, index){ return Object.values(obj.data["Monthly Time Series"][key]); });

获取所有值(虽然它们不会被连接起来)


如何访问“Monthly time series”中“1.open”的值?如果我重复上述方法,它会显示未定义。 - Aayushi
1. open isn't within Monthly time series - it's data['data']['Monthly Time Series']['2017-07-31']['1. open'] - eithed
我做了类似这样的事情:Object.keys(response.data['Monthly Time Series']).forEach(function(k){ console.log(Object.keys(k["1. open"])); 它在控制台上显示未定义。 - Aayushi
请说明您想要做什么 - 目前您正在获取forEach循环结果的键,这实际上返回undefinedforEach始终返回undefined)。您是否想要获取“月度时间序列”中最低级别的值? - eithed
是的,我想获取每月时间序列中最低级别的值。 - Aayushi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接