javascript 从嵌套的 json 中获取键的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17941127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:11:22  来源:igfitidea点击:

Get value of key from a nested json

javascriptjqueryjsonnested

提问by Okky

I have a (nested) data structure containing objects and arrays. How can I extract the information, i.e. access a specific or multiple values with help of a known key?

我有一个包含对象和数组的(嵌套)数据结构。如何提取信息,即在已知密钥的帮助下访问特定或多个值?

For example:

例如:

var data = {
    code: 42,
    items: [{
        id: 1,
        category: [{
            cId: 1,
            prodname: 'foo',
            quality: [{
                qId: 012,
                testName: 'micro'
            }, {
                qId: 013,
                testName: 'nano'
            }]
        }, {
            id: 2,
            prodname: 'bar'
        }]
    }]
};

How could I access the value of key quality?

我怎样才能访问 key 的值quality

Note: This is a sample JSON object the object is dynamically generated; it is of unknown depth.

注意:这是一个示例 JSON 对象,该对象是动态生成的;它的深度未知。

采纳答案by oleq

That way is correct:

这种方式是正确的:

data.items[ 0 ].category[ 0 ].quality;
> [ Object, Object ]