admin管理员组

文章数量:1394544

I am trying to get the first id value in results array from that object I got from an API:

{
    "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 munity 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 overe 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
        }
    ]
}

Lets say my json object called json, What i tried so far is:

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

and I got the following error: TypeError: Cannot read property '0' of undefined.

I can't figure out what seems to be the problem, Is there other way to get the first id value in JavaScript?

I am trying to get the first id value in results array from that object I got from an API:

{
    "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 munity 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 overe 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
        }
    ]
}

Lets say my json object called json, What i tried so far is:

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

and I got the following error: TypeError: Cannot read property '0' of undefined.

I can't figure out what seems to be the problem, Is there other way to get the first id value in JavaScript?

Share Improve this question asked Aug 29, 2015 at 8:59 MoshikMoshik 511 gold badge2 silver badges8 bronze badges 4
  • Is it still in string form? If so, use JSON.parse(json) to convert it to Object first. – fuyushimoya Commented Aug 29, 2015 at 9:01
  • if json is a JAVASCRIPT object, that should work - if it's a json STRING then you need to JSON.parse first – Jaromanda X Commented Aug 29, 2015 at 9:01
  • check this example – J Santosh Commented Aug 29, 2015 at 9:09
  • the problem is that it's not a string – Moshik Commented Aug 29, 2015 at 9:12
Add a ment  | 

2 Answers 2

Reset to default 3

Well, this should be so simple. If you keep JSON data in a Javascript variable, then the way you tried should work. See my version of it.

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 munity 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 overe 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);

It is a better practice to check the datatype of the variable one is about to parse using typeOf json==='object', if it is a valid object then you can do further manipulations or else parse it using json=JSON.parse(json);

本文标签: JavaScriptHow to get a specific value from a JSON objectStack Overflow