Skip to content

Instantly share code, notes, and snippets.

@1995eaton
Created July 2, 2014 19:57
Show Gist options
  • Save 1995eaton/43403db6f4a4e8e25f6b to your computer and use it in GitHub Desktop.
Save 1995eaton/43403db6f4a4e8e25f6b to your computer and use it in GitHub Desktop.
ESPN
// You can copy and paste this into your browser's developer console to navigate through the JSON response
function espn(path, key, callback) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://api.espn.com/v1' + path + '?apikey=' + key);
xhr.addEventListener('readystatechange', function() {
if (xhr.readyState === 4 && xhr.status === 200) {
callback(JSON.parse(xhr.responseText));
}
});
xhr.send();
}
// Create a global variable for the JSON so
// we can easily look through the data to find
// the information we want
var response;
// Get the first 50 NBA athletes using out API key
espn('/sports/basketball/nba/athletes', 'xqkm37pcb83kvfkqjwsh4984', function(data) {
// Assign the response to the global variable
response = data;
// In this case, this array is where the athletes are located
athletes = data.sports[0].leagues[0].athletes;
console.log(athletes.map(function(e) {
// Filter through the athletes, only showing their display names
return e.displayName;
}));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment