簡體   English   中英

JavaScript-如何從JSON對象獲取特定值

[英]JavaScript - How to get a specific value from a JSON object

我試圖從我從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值嗎?

好吧,這應該是如此簡單。 如果將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);

更好的做法是使用typeOf json ==='object'檢查要解析的變量的數據類型,如果它是有效的對象,則可以進行進一步的處理,或者使用json = JSON.parse( json);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM