Test.Customers = [{
"CustomerPortals":[{
"filename":"chrysanthemum.jpg",
"ext":"jpg",
},{
"name":"Lighthouse.jpg",
"filename":"lighthouse.jpg",
"ext":"jpg",
},{
"filename":"lighthouse.jpg",
"ext":"jpg",
}
}]
我将这个json存储在我的DOM中。有时在CustomerPortals数组中将有0个项目,有时还有多个项目。但我想解析它们并显示文件名。 jquery是否更容易做到这一点?
修改
修改
var Test = {};
Test.Customers = [{"CustomerItems":
"Portals":
{"id":"1","customer":"1950","resident":"yes",
"CustomPortals":
[
{"id":"11","test":"4251","portalname":"tye.jpg"},
{"id":"12","test":"4251","portalname":"Lsdf.jpg"},
{"id":"13","test":"4251","portalname":"nick.jpg"}
]
}
},
{"CustomerItems":
"Portals":
{"id":"2","customer":"1952","resident":"yes",
"CustomPortals":
[
{"id":"14","test":"4252","portalname":"Chrysanthemum2.jpg"},
{"id":"15","test":"4255","portalname":"navagin.jpg"},
{"id":"16","test":"4257","portalname":"jasoria.jpg"}
]
}
},
{"CustomerItems":
"Portals":
{"id":"3","customer":"1950","resident":"yes",
"CustomPortals":
[
{"id":"17","test":"4231","portalname":"Zsryanmum1.jpg"},
{"id":"18","test":"4651","portalname":"Ltd1.jpg"},
{"id":"19","test":"4281","portalname":"ser1.jpg"}
]
}
}
]
我想获得所有人的门户名称 顾客们。以上不是 显示子阵列谢谢
答案 0 :(得分:1)
我对此表示怀疑。 JSON对象层次结构不作为DOM节点可见。
您可以使用地图功能,该功能非常容易write,并且可以在Functional JavaScript等库中随时使用。使用它很简单:
filenames = Test.Customers[0].CustomerPortals.map(function(cp) { return cp.filename });
然后用文件名数组做任何你想做的事。
编辑:为了确认这一点,我在JavaScript test bench中运行了以下代码。
Test = {};
Test.Customers = [{
"CustomerPortals":[{
"filename":"chrysanthemum.jpg",
"ext":"jpg",
},{
"name":"Lighthouse.jpg",
"filename":"lighthouse.jpg",
"ext":"jpg",
},{
"filename":"lighthouse.jpg",
"ext":"jpg",
}]
}];
Array.prototype.map = function(fn) {
var r = [];
var l = this.length;
for(i=0;i<l;i++)
{
r.push(fn(this[i]));
}
return r;
};
alert(Test.Customers[0].CustomerPortals.map(function(cp) { return cp.filename }));
它显示一条警告,其中包含以下消息:
chrysanthemum.jpg,lighthouse.jpg,lighthouse.jpg
答案 1 :(得分:1)
我唯一可以想到的是jquery可以帮助你的是.each()
函数。这是一个我浏览每个客户的每个文件名的例子。
更新了代码以包含保存CustomerPortals的门户网站。如果我错误地解释了您的评论,请与我们联系。
再次更新代码以使用新的示例代码。示例代码导致错误,因此我在“门户”及其值附近添加了{}。如果这不是代码的预期格式,请告诉我。
var Test = {};
Test.Customers = [{"CustomerItems":
{"Portals":
{"id":"1","customer":"1950","resident":"yes",
"CustomPortals":
[
{"id":"11","test":"4251","portalname":"tye.jpg"},
{"id":"12","test":"4251","portalname":"Lsdf.jpg"},
{"id":"13","test":"4251","portalname":"nick.jpg"}
]
}
}
},
{"CustomerItems":
{"Portals":
{"id":"2","customer":"1952","resident":"yes",
"CustomPortals":
[
{"id":"14","test":"4252","portalname":"Chrysanthemum2.jpg"},
{"id":"15","test":"4255","portalname":"navagin.jpg"},
{"id":"16","test":"4257","portalname":"jasoria.jpg"}
]
}
}
},
{"CustomerItems":
{"Portals":
{"id":"3","customer":"1950","resident":"yes",
"CustomPortals":
[
{"id":"17","test":"4231","portalname":"Zsryanmum1.jpg"},
{"id":"18","test":"4651","portalname":"Ltd1.jpg"},
{"id":"19","test":"4281","portalname":"ser1.jpg"}
]
}
}
}
]
$(document).ready(function() {
//Go through each customer
$.each(Test.Customers, function(customerIndex, customerValue) {
//Go through each CustomerPortal and display filename
$.each(customerValue.CustomerItems.Portals.CustomPortals, function(custPortIndex, custPortValue) {
alert('File ' + custPortValue.portalname + ' in customer ' + customerIndex);
});
});
});
修改强>
var Test = {};
Test.Customers = [{"CustomerItems":
"Portals":
{"id":"1","customer":"1950","resident":"yes",
"CustomPortals":
[
{"id":"11","test":"4251","portalname":"tye.jpg"},
{"id":"12","test":"4251","portalname":"Lsdf.jpg"},
{"id":"13","test":"4251","portalname":"nick.jpg"}
]
}
},
{"CustomerItems":
"Portals":
{"id":"2","customer":"1952","resident":"yes",
"CustomPortals":
[
{"id":"14","test":"4252","portalname":"Chrysanthemum2.jpg"},
{"id":"15","test":"4255","portalname":"navagin.jpg"},
{"id":"16","test":"4257","portalname":"jasoria.jpg"}
]
}
},
{"CustomerItems":
"Portals":
{"id":"3","customer":"1950","resident":"yes",
"CustomPortals":
[
{"id":"17","test":"4231","portalname":"Zsryanmum1.jpg"},
{"id":"18","test":"4651","portalname":"Ltd1.jpg"},
{"id":"19","test":"4281","portalname":"ser1.jpg"}
]
}
}
]
我想获得所有人的门户名称 顾客们。以上不是 显示子阵列谢谢