Created
July 2, 2014 19:57
-
-
Save 1995eaton/43403db6f4a4e8e25f6b to your computer and use it in GitHub Desktop.
ESPN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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