1

I know this is a noob question but I'm new to json... I cant access this object data:

    {
 "kind": "youtube#channelListResponse",
 "etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/7zZjjC0N0XTk8OrPCzfx2O9vPg8\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#channel",
   "etag": "\"Y3xTLFF3RLtHXX85JBgzzgp2Enw/_uCWU9q9VCvKwgXG_6vL636QCVU\"",
   "id": "UCSWgmaFWOuYVWR8Z30n5qLQ",
   "snippet": {
    "title": "ChrisCodeX",
    "description": "Channel Features: wateva\r\n-music\r\n-gaming\r\n-comedy\r\nSubscribe to stay tune!\r\n\r\nFun Fact, to the haters out there:\r\n\r\nBeing an xbox fan isn't wrong but I hope you're being sarcastic and you realize that's a myth evolved from peoples' insistance on proving they were getting their money's worth from XBL.\r\nThe \"online connection\" is determined solely by your personal internet speeds. In other words it has nothing to do with which console you play.",
    "publishedAt": "2011-08-09T02:23:58.000Z",
    "thumbnails": {
     "default": {
      "url": "https://yt3.ggpht.com/-UL6VyOBij08/AAAAAAAAAAI/AAAAAAAAAAA/Y4oSGlkvucw/s88-c-k-no/photo.jpg"
     },
     "medium": {
      "url": "https://yt3.ggpht.com/-UL6VyOBij08/AAAAAAAAAAI/AAAAAAAAAAA/Y4oSGlkvucw/s240-c-k-no/photo.jpg"
     },
     "high": {
      "url": "https://yt3.ggpht.com/-UL6VyOBij08/AAAAAAAAAAI/AAAAAAAAAAA/Y4oSGlkvucw/s240-c-k-no/photo.jpg"
     }
    },
    "localized": {
     "title": "ChrisCodeX",
     "description": "Channel Features: wateva\r\n-music\r\n-gaming\r\n-comedy\r\nSubscribe to stay tune!\r\n\r\nFun Fact, to the haters out there:\r\n\r\nBeing an xbox fan isn't wrong but I hope you're being sarcastic and you realize that's a myth evolved from peoples' insistance on proving they were getting their money's worth from XBL.\r\nThe \"online connection\" is determined solely by your personal internet speeds. In other words it has nothing to do with which console you play."
    }
   }
  }
 ]
}

How will I get the value from thumbnails default url?

I tried with

$-getJSON(url, function(data){
var url = data.items.snippet.thumbnails.default.url;
});

But all I get is error TypeError: data.items[0].snippet.thumbnail is undefined

The Bumpaster
  • 944
  • 7
  • 20
  • 1
    Well, `thumbnail` is indeed undefined. `thumbnails` is not. The code you posted and the message you posted don't match. – Felix Kling Jun 20 '15 at 08:54
  • @FelixKling Of course they don't match, due to that I did not copy the response, I typed it... But you get the point of my question, don't you? – The Bumpaster Nov 04 '17 at 12:36

1 Answers1

2

The items property in your object is an array, so you have to define an index when accessing it:

data.items[0].snippet.thumbnails.default.url
redelschaap
  • 2,774
  • 2
  • 19
  • 32
  • How do you know when smth is an array or object? – The Bumpaster Jun 20 '15 at 12:58
  • 1
    The items property looks like this: `"items": [{ "kind": "youtube#channel", ... }]`. Note the outer `[{}]`. All other properties are wrapped by just `{}`. `{...}` represents an object, while `[{...}]` represents an array of objects. In this case you have only one item. More items are comma separated: `[{...}, {...}]`. – redelschaap Jun 20 '15 at 13:06
  • Thank you very much :* I'll keep that on my mind next time :) – The Bumpaster Jun 20 '15 at 16:55