Jump to content

$.eachJSON with multidimensional array


Lukeidiot

Recommended Posts

Hello I am trying to output the titles of each of the 10 results provided in the following JSON URL: https://www.googleapis.com/youtube/v3/search?part=snippet&q=test&maxResults=10&key=AIzaSyCoSXBJjRh7ToEJIMH2QF1-UsgK71E_RQ4

 

 

function populateRelated(searchTerm){

var search_input = searchTerm;
var keyword = encodeURIComponent(search_input);
var yt_url2 = 'https://www.googleapis.com/youtube/v3/search?part=snippet&q=' + keyword + '&maxResults=10&key=AIzaSyCoSXBJjRh7ToEJIMH2QF1-UsgK71E_RQ4';

$.getJSON(yt_url2, function(data) {

$.each(data, function(i,v){
$('#relatedsongs').append('<a class="dl" href="#" style="font-size: 12px;">' + items[0].id.videoId + '</a><br>');
console.log('worked');
});

});

}

The upper code DOES NOT work. Any idea how I can get it to output the titles for the results only?

 

Example
Video Title 1

Video Title 2

Video Title 3

...

Video Title 10

 

Thanks.

 

The data is the entire response, not just a part of it. The items array is at .items and that's what you should be looping over. Inside the function, v will be one of the items and you should use .id.videoId on that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.