Zrth
Zrth

Reputation: 38

A very simple JSON request with jQuery

I'm trying to get a simple JSON request working with jQuery. I want to get the premiers of new shows from Trakt (API: http://trakt.tv/api-docs/calendar-premieres), but cannot navigate through the nodes. Seems that I might be needing a date attached after the API key in the URL, but I'm not sure, since dealing with JSON is quite new to me.

    $.ajax({
      type: "GET",
      url: [url_with_actual_api_key],
      dataType: "json",
      async: true,
      cache: false,
      success: function(data){ 
        $.each(data, function(data, value) {
            $(".title").append("<div class='item'><span>Title:</span>" + value.title + "</div>");
        });
      }  

I'm getting UNDEFINED of course, because I'm trying to retrieve data few levels 'deeper'. Also, value.episode shows nothing, getting the title is the closest I got. My question is, how do I get the whole data from the request for each episode on API's date?

Thanks a lot and a million highfives.

Upvotes: 0

Views: 478

Answers (1)

user1864610
user1864610

Reputation:

You need to iterate twice, since you have an array of date, containing an array of episodes:

success: function(data){ 
    // for each date
    $.each(data, function(index, broadcastDate) {

        // broadcastDate.date has the date
        // broadcastDate.episodes has the show data.

        $.each(broadcastDate.episodes, function(index,episode) {

          // episode contains show and episode objects

          $(".title").append("<div class='item'><span>Title:</span>" + episode.show.title + "</div>");
        });
    });

Upvotes: 1

Related Questions