2014-10-17 124 views
0

我有以下的JSON:AngularJS解析JSON

http://localhost:3000/folder/1234567

[ 

    { 

     "_id": "543e95d78a1cec2a38ed53ec" 

    }, 

    { 

     "_id": "543e95d78a1cec2a38ed53f1" 

    } 

] 

,我想所有的_id。

所以,我做这样的事情:

$http.get('http://localhost:3000/folder/' + folderId) 
         .success(function (response) { 
        console.log("response"+ response); 
        console.log("response id"+ response.id); 

但我有:

response [object Object],[object Object] 
response id undefined 

如何解决获得_id?

感谢您的帮助!

+0

的响应是一个数组,所以你必须遍历它。你需要哪个'_id'? – razvanz 2014-10-17 08:17:47

+0

如何处理数组?所有的_ids。谢谢! – Jose 2014-10-17 08:19:40

+0

这取决于你想要用你的数据做什么。如果您想要显示它们,只需将数据放到您的范围并在您的视图中使用ng-repeat。或者如果你想做别的事情,你可以使用angulars左右。 – 2014-10-17 08:25:17

回答

2

要获得所有的ID以相反的顺序:

var allIds = []; 

$http.get('http://localhost:3000/folder/' + folderId).success(function(res) { 
    for (var i = res.length-1; i>-1; i--) { 
     allIds.push(res[i]._id); 
    } 
    // this prints an array with all the Ids 
    console.log(allIds); 
});