2011-04-03 64 views
1

我有一個JSON varJavaScript的JSON多個按鍵

{ 
"total":4, 
"page":1, 
"pagesize":30, 
"type":"associated_user", 
"items":[ 
{ 
"user_id":2727, 
"site_name":"Physics", 
"user_creation_date":1300894372, 
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade" 
}, 
{ 
"user_id":3864, 
"site_name":"TeX - LaTeX", 
"user_creation_date":1298808016, 
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade" 
}, 
{ 
"user_id":5983, 
"site_name":"Stack Apps", 
"user_creation_date":1301323608, 
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade" 
}, 
{ 
"user_id":6260, 
"site_name":"English Language and Usage", 
"user_creation_date":1300449905, 
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade" 
} 
] 
} 

正如你所看到的,也有items塊中有四個site_name鍵。我只能訪問第一個使用items[0].site_name但如何訪問其他三個一個一個?

+1

當你使用'items [1] .site_name'時,你會得到什麼? – 2011-04-03 11:49:48

+0

done ........... – yolo 2011-04-03 11:51:56

+0

你應該接受一個答案...... – 2011-04-03 11:53:47

回答

1

問候 如果我讓你正確,所以你需要循環對象陣列就像這樣

var obj ={ 
"total":4, 
"page":1, 
"pagesize":30, 
"type":"associated_user", 
"items":[ 
{ 
"user_id":2727, 
"site_name":"Physics", 
"user_creation_date":1300894372, 
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade" 
}, 
{ 
"user_id":3864, 
"site_name":"TeX - LaTeX", 
"user_creation_date":1298808016, 
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade" 
}, 
{ 
"user_id":5983, 
"site_name":"Stack Apps", 
"user_creation_date":1301323608, 
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade" 
}, 
{ 
"user_id":6260, 
"site_name":"English Language and Usage", 
"user_creation_date":1300449905, 
"association_id":"2fbcabe5-cd42-4f5b-821e-2f1475e42ade" 
} 
] 
}; 

var objItems = obj.items; 
for(var i=0;i<objItems .length;i++) 
{ 
alert(obj.items[i].site_name); 
} 
0

你可以很容易地用jQuery做到這一點:

$(jsonvar)。每個(函數(){
的TempVar = this.site_name;
});

然後通過存放在「TempVar的」所需段的項目,將循環

1

爲了讓所有的站點名稱

你需要運行一個while loop做動態

var i=0 
while(i<items.length){ 

var site_name=items[i].site_name; 
}