JavaScript - 如何从JSON对象获取特定值

时间:2015-08-29 08:59:55

标签: javascript json

我试图从我从API获得的对象中获取结果数组中的第一个id值:

{
    "page" : 1,
    "results" : [{
            "adult" : false,
            "backdrop_path" : "/fLL6WfUXvdQee1fD4xuzNnWfVBk.jpg",
            "genre_ids" : [27, 9648, 80],
            "id" : 176,
            "original_language" : "en",
            "original_title" : "Saw",
            "overview" : "Obsessed with teaching his victims the value of life, a deranged, sadistic serial killer abducts the morally wayward. Once captured, they must face impossible choices in a horrific game of survival. The victims must fight to win their lives back, or die trying...",
            "release_date" : "2004-01-19",
            "poster_path" : "/dHYvIgsax8ZFgkz1OslE4V6Pnf5.jpg",
            "popularity" : 2.897462,
            "title" : "Saw",
            "video" : false,
            "vote_average" : 7.1,
            "vote_count" : 657
        }, {
            "adult" : false,
            "backdrop_path" : "/yKATxJtGY67cXdOmlbWwW6EgPqn.jpg",
            "genre_ids" : [27, 53, 80],
            "id" : 663,
            "original_language" : "en",
            "original_title" : "Saw IV",
            "overview" : "Jigsaw and his apprentice Amanda are dead. Now, upon the news of Detective Kerry's murder, two seasoned FBI profilers, Agent Strahm and Agent Perez, arrive in the terrified community to assist the veteran Detective Hoffman in sifting through Jigsaw's latest grisly remains and piecing together the puzzle. However, when SWAT Commander Rigg is abducted and thrust into a game, the last officer untouched by Jigsaw has but ninety minutes to overcome a series of demented traps and save an old friend...or face the deadly consequences.",
            "release_date" : "2007-10-25",
            "poster_path" : "/veApHw5ARGHWf3ptKf30rOGFY9n.jpg",
            "popularity" : 2.449196,
            "title" : "Saw IV",
            "video" : false,
            "vote_average" : 5.8,
            "vote_count" : 257
        }
    ]
}

让我说我的json对象叫json,到目前为止我尝试的是:

console.log(json.results[0].id);

我收到以下错误: TypeError:无法读取未定义的属性“0”。

我无法弄清楚似乎是什么问题,还有其他方法可以在JavaScript中获取第一个id值吗?

2 个答案:

答案 0 :(得分:3)

嗯,这应该是这么简单。如果将JSON数据保存在Javascript变量中,那么您尝试的方式应该可行。看我的版本。

var jsonData = {
    "page" : 1,
    "results" : [{
            "adult" : false,
            "backdrop_path" : "/fLL6WfUXvdQee1fD4xuzNnWfVBk.jpg",
            "genre_ids" : [27, 9648, 80],
            "id" : 176,
            "original_language" : "en",
            "original_title" : "Saw",
            "overview" : "Obsessed with teaching his victims the value of life, a deranged, sadistic serial killer abducts the morally wayward. Once captured, they must face impossible choices in a horrific game of survival. The victims must fight to win their lives back, or die trying...",
            "release_date" : "2004-01-19",
            "poster_path" : "/dHYvIgsax8ZFgkz1OslE4V6Pnf5.jpg",
            "popularity" : 2.897462,
            "title" : "Saw",
            "video" : false,
            "vote_average" : 7.1,
            "vote_count" : 657
        }, {
            "adult" : false,
            "backdrop_path" : "/yKATxJtGY67cXdOmlbWwW6EgPqn.jpg",
            "genre_ids" : [27, 53, 80],
            "id" : 663,
            "original_language" : "en",
            "original_title" : "Saw IV",
            "overview" : "Jigsaw and his apprentice Amanda are dead. Now, upon the news of Detective Kerry's murder, two seasoned FBI profilers, Agent Strahm and Agent Perez, arrive in the terrified community to assist the veteran Detective Hoffman in sifting through Jigsaw's latest grisly remains and piecing together the puzzle. However, when SWAT Commander Rigg is abducted and thrust into a game, the last officer untouched by Jigsaw has but ninety minutes to overcome a series of demented traps and save an old friend...or face the deadly consequences.",
            "release_date" : "2007-10-25",
            "poster_path" : "/veApHw5ARGHWf3ptKf30rOGFY9n.jpg",
            "popularity" : 2.449196,
            "title" : "Saw IV",
            "video" : false,
            "vote_average" : 5.8,
            "vote_count" : 257
        }
    ]
};

console.log(jsonData.results[0].id);
console.log(jsonData.results[1].id);

答案 1 :(得分:1)

更好的做法是使用 typeOf json ===' object' 检查要解析的变量的数据类型,如果它是有效对象那么你可以做进一步的操作,或者使用 json = JSON.parse(json);

解析它