从JSON检索特定对象

时间:2017-06-24 07:27:07

标签: json node.js

这是TMDB返回的JSON文件的片段,我尝试访问每个对象的标题。我已尝试使用以下方法,例如此帖How to access specific value from a nested array within an object array

"results": [
    {
      "vote_count": 2358,
      "id": 283366,
      "video": false,
      "vote_average": 6.5,
      "title": "Miss Peregrine's Home for Peculiar Children",
      "popularity": 20.662756,
      "poster_path": "/AvekzUdI8HZnImdQulmTTmAZXrC.jpg",
      "original_language": "en",
      "original_title": "Miss Peregrine's Home for Peculiar Children",
      "genre_ids": [
        18,
        14,
        12
      ],
      "backdrop_path": "/9BVHn78oQcFCRd4M3u3NT7OrhTk.jpg",
      "adult": false,
      "overview": "A teenager finds himself transported to an island where he must help protect a group of orphans with special powers from creatures intent on destroying them.",
      "release_date": "2016-09-28"
    },
    {
      "vote_count": 3073,
      "id": 381288,
      "video": false,
      "vote_average": 6.8,
      "title": "Split",
      "popularity": 17.488396,
      "poster_path": "/rXMWOZiCt6eMX22jWuTOSdQ98bY.jpg",
      "original_language": "en",
      "original_title": "Split",
      "genre_ids": [
        27,
        53
      ],
      "backdrop_path": "/4G6FNNLSIVrwSRZyFs91hQ3lZtD.jpg",
      "adult": false,
      "overview": "Though Kevin has evidenced 23 personalities to his trusted psychiatrist, Dr. Fletcher, there remains one still submerged who is set to materialize and dominate all the others. Compelled to abduct three teenage girls led by the willful, observant Casey, Kevin reaches a war for survival among all of those contained within him — as well as everyone around him — as the walls between his compartments shatter apart.",
      "release_date": "2016-11-15"
    },

3 个答案:

答案 0 :(得分:1)

var titles = results.map(function extract(item){return item.title})

map函数遍历数组并通过在每个项目上应用extract函数来构建结果数组。

答案 1 :(得分:0)

for (var i =0; i < obj.results.length; i++) {
 console.log(obj.results[i].title);
}

首先,我们得到结果键,然后迭代它,因为它是一个数组。

答案 2 :(得分:0)

结果只不过是一个对象数组,没有任何嵌套。 您可以使用.forEach()

results.forEach(function(item){
  console.log(item.title);
});