Jump to content

$.eachJSON with multidimensional array


Lukeidiot
Go to solution Solved by requinix,

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.

 

Link to comment
Share on other sites

  • Solution

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.