Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jquery中读取此JSON_Javascript_Jquery_Arrays_Json - Fatal编程技术网

Javascript 如何在jquery中读取此JSON

Javascript 如何在jquery中读取此JSON,javascript,jquery,arrays,json,Javascript,Jquery,Arrays,Json,我有这个JSON,我不知道如何访问productList中的元素。这是JSON [ { "id": 1, "name": "sala1", "deleted": "NODELETED", "sectionList": [ { "id": 1, "name": "seccion1", "methodList": [], "frecuencyList": [

我有这个JSON,我不知道如何访问productList中的元素。这是JSON

[
{
    "id": 1,
    "name": "sala1",
    "deleted": "NODELETED",
    "sectionList": [
        {
            "id": 1,
            "name": "seccion1",
            "methodList": [],
            "frecuencyList": [],
            "productList": [
                {
                    "id": 1,
                    "name": "lejia",
                    "deleted": "NODELETED"
                },
                {
                    "id": 3,
                    "name": "agua",
                    "deleted": "NODELETED"
                },
                {
                    "id": 4,
                    "name": "cal",
                    "deleted": "NODELETED"
                }
            ],
            "deleted": "NODELETED"
        },
        {
            "id": 2,
            "name": "seccion2",
            "methodList": [],
            "frecuencyList": [],
            "productList": [
                {
                    "id": 1,
                    "name": "lejia",
                    "deleted": "NODELETED"
                }
            ],
            "deleted": "NODELETED"
        }
    ]
}
]

我用这个:

$.getJSON('my url' , function(data) {

$.each(data , function(key, val)
{
    console.log("Value of ProductList-> " + data['productList'].name );
});

}); 

这是我第一次在其他数组中使用JSON,我很迷茫。谢谢大家

data[0]。sectionList[0]。productList
将为您提供集合中的第一项其
sectionList
集合的
productList
中的第一项

$.getJSON('my url' , function(data) {

   $.each(data , function(key, val)
   {
    console.log("Value of ProductList-> " + val.sectionList[0].productList.name );
   });

}); 
要获得每个产品列表,您需要另一个内部循环

   $.getJSON('my url' , function(data) {

   $.each(data , function(key, val)
   {
    $.each(val.sectionList , function(k, v){
      console.log(v.productList.name);
    }
   });

}); 

如果要避免使用jQuery循环,只需使用vanilla JS即可:

var productList = data[0].sectionList[0].productList;

for (var i = 0, l = productList.length; i < l; i++) {
  console.log('Value of ProductList-> ' + productList[i].name);
}
var productList=data[0]。sectionList[0]。productList;
对于(var i=0,l=productList.length;i'的值+ProductList[i].name);
}
您也可以尝试以下方法:

jQuery.each(data , function(key, val)
{
    jQuery.each(val['sectionList'] , function(sectionIndex, section){
        jQuery.each(section['productList'] , function(index, product){
            alert("Value of ProductList-> " + product.name );
        });                
    });

});
可能重复的