我知道这对你们中的一些人来说似乎很基本,但我试图用jquery迭代一个json对象,如何到达每个生成器的每个网页名称。请帮忙! 在底部是我的代码,欢迎任何帮助。
{
"producers": [
{
"producer": {
"id": "1",
"name": "Radosa",
"address": " Grenvägen 1-3",
"zipcode": " 577 39",
"district": " Hultsfred",
"webpage": [
{
"name": "waste",
"purpose": "sample "
},
{
"name": "Ultra Worker",
"purpose": "Posao"
}
],
"logoURL": "../producenter/images/ema.png",
"latitude": "57.4999",
"longitude": "15.828"
}
},
{
"producer": {
"id": "2",
"name": "Marko",
"address": " Grenvägen 1-3",
"zipcode": " 577 39",
"district": " Hultsfred",
"webpage": [
{
"name": "Sample name",
"purpose": "sample purpose"
},
{
"name": "Sample name",
"purpose": "sample purpose"
}
],
"logoURL": "../producenter/images/ema.png",
"latitude": "57.4999",
"longitude": "15.828"
}
}
]
}
$.each(data.producers, function(key, value) {
console.log(value.producer); ------ this works,
console.log(value.webpage.name); ------ this don't work. How to reach this value?
});
答案 0 :(得分:2)
console.log(value.webpage.name);
应该是
// webpage is an array too, get the first element of it for example
console.log(value.producer.webpage[0].name);
答案 1 :(得分:1)
说你的json对象是数据
试试这个
$.each(data.producers,function(i,v){
$.each(v.producer.webpage,function(i1,v1){ //<--since webpage is an array, use each again...
alert(v1.name);
})
});
或者只是你可以使用索引..
$.each(data.producers,function(index,value){
console.log(value.producer.webpage[0].name); //first name
console.log(value.producer.webpage[0].name); //second name
});
答案 2 :(得分:0)
由于value.producer.webpage
是array
,您需要:
for(var i=0; i<value.producer.webpage.length; i++) {
var webpage = value.producer.webpage[i];
console.log(webpage.name);
}
答案 3 :(得分:0)
Sou可以这样做:
for(var producer in Object.producers){
if(Object.producers.hasOwnProperty(producer) && Object.producers[producer].webpage!==undefined){
for(var i=0; i<Object.producers[producer].webpage.length; i+=1){
// Your further code here
}
}
}