2015-02-11 105 views
0

這裏是我的JSON數據:jQuery中遍歷JSON數據

{ 
    "comments": [{ 
     "id": "1", 
     "message": "Finish as soon as possible! Cibai!", 
     "task_id": "1", 
     "user_id": "1", 
     "date_created": "2015-02-06 00:00:00.000000" 
    }, { 
     "id": "19", 
     "message": "Another message", 
     "task_id": "1", 
     "user_id": "1", 
     "date_created": "2015-02-10 00:00:00.000000" 
    }, { 
     "id": "20", 
     "message": "Comment about the header", 
     "task_id": "1", 
     "user_id": "1", 
     "date_created": "2015-02-09 00:00:00.000000" 
    }], 
     "status": true 
} 

這裏是我的jQuery的,問題是蔭的警報越來越空:

var ids = []; 
$.each(e, function(i, item) { 
    ids.push(item.id); 
}); 
alert(JSON.stringify(ids)); 

感謝

回答

3

id屬性是存儲在comments陣列中的對象的一部分。您需要循環使用e.comments。此外,當你正在構建一個數組,你可以使用map()代替each()

var ids = $.map(e.comments, function(item) { 
    return item.id; 
}); 
alert(JSON.stringify(ids)); 

Example fiddle