1

我有以下 JSON 对象。

{"feed":[
    {"news":
      {"adopted_from":null,"user_id":null,"description":"this is test","id":2}
   },
   {"news":
     {"adopted_from":null,"user_id":null,"description":"like unlike done","id":1}
    }
]}

我想检索新闻的ID。我尝试了许多不同的方式(例如feed[0].news.id, feed.news.id, feed[[0].news.id])但无法访问该值。谁能帮助我如何使用 JavaScript 访问它?

4

3 回答 3

2

我从上面复制并粘贴了您的 JSON 并尝试了以下操作,效果很好:


    var data = {"feed":[{"news":{"adopted_from":null,"user_id":null,"description":"this is test","id":2}},{"news":{"adopted_from":null,"user_id":null,"description":"like unlike done","id":1}}]};
    // alert the first news id
    alert(data.feed[0].news.id);

news它按预期从数组中的第一个对象获取 id 。

于 2011-09-22T10:01:21.427 回答
1

这对我有用:

var f = {"feed":[{"news":{"adopted_from":null,"user_id":null,"description":"this is test","id":2}},{"news":{"adopted_from":null,"user_id":null,"description":"like unlike done","id":1}}]}
alert( f.feed[0].news.id )
于 2011-09-22T10:03:00.113 回答
0
var feed = json_decode(yourData)
for(var counter in feed) {
    console.log(feed[counter].news.id);
}
于 2011-09-22T10:25:39.480 回答