如何读取多级json [英] How to read multi-level json

查看:122
本文介绍了如何读取多级json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我知道如何读取单级" json数组.但是,我无法弄清楚如何从多层json数组中索引所需的值.

So, I know how to read a "single-level" json array. However, I can't figure out how to index the value I want from a multi-level json array.

我有以下JSON数据:

I have this JSON data:

{
    "items": [
        {
            "snippet": {
                "title": "YouTube Developers Live: Embedded Web Player Customization"
            }
        }
    ]
}

我想访问title的值,但是这两个都不访问该值,而是返回undefined:

I would like to access the value of title, however neither of these access the value, but instead return undefined:

console.log(data["items"][0].title);

或:

console.log(data["items"][0]["title"]);

但是,此代码返回snippet对象:

But, this code returns the snippet object:

console.log(data["items"][0]);

data变量引用json数据.

The data variable refers to the json data.

我应该怎么做?

推荐答案

尝试一下:

data.items[0].snippet.title

说明(您可以在/* */注释中看到相应的对象):

Explanation (you can see corresponding object in /* */ comment):

items[0];
/*
{
    'snippet': {
        'title': 'YouTube Developers Live: Embedded Web Player Customization'
    }
}
*/

items[0].snippet;
/*
{
    'title': 'YouTube Developers Live: Embedded Web Player Customization'
}
*/

items[0].snippet.title;
/*
'YouTube Developers Live: Embedded Web Player Customization'
*/

这篇关于如何读取多级json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆