Javascript 如何在jquery中读取此JSON
我有这个JSON,我不知道如何访问productList中的元素。这是JSONJavascript 如何在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": [
[
{
"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 );
});
});
});
可能重复的