Loading External JSON into your Application
For our example, we are going to use the Twitter API. For those of you who haven't tried to interface with Twitter, it is extremely easy to comprehend. Let's say that you absolutely adore me. So much, in fact, that you would love to put my latest tweets on your very own web site. Bless your heart.
Step 1 - Get the Object
If you look over the Twitter API, you can read over all of the wonderful method calls they provide for you, including one that lets you retrieve a user's timeline in JSON format. The format of the URI is as follows:
http://twitter.com/statuses/user_timeline/franklakatos.json
If you try to load this URI in your browser, it will prompt you to download a file. Why a file, and what is inside of it? Since JSON is a front-end technology, it would violate Cross-Site Scripting (XSS) rules to make calls to another domain using the XMLHttpRequest object, the object that powers other asynchronous calls as with AJAX. To work around doing this, you will include this file call in your HTML as a javascript include file:
The API request commonly makes available a varying number of parameters to control the output of your request, such as a limit to how many to return.
If you make a call to this JSON file, you will be returned a file that contains an object that looks like this:
- [{
- "text":"Just handed in my last project ever for my masters, all I have now is finals next week. Goodbye, school. Hello, life.",
- "in_reply_to_screen_name":null,
- "user":{
- "profile_background_color":"9ae4e8",
- "followers_count":10,
- "profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/110871907\/me_copy_normal.jpg",
- "description":null,
- "utc_offset":null,
- "friends_count":6,
- "profile_text_color":"000000",
- "screen_name":"franklakatos",
- "following":null,
- "profile_background_image_url":"http:\/\/static.twitter.com\/images\/themes\/theme1\/bg.gif",
- "profile_link_color":"0000ff",
- "url":null,
- "name":"franklakatos",
- "favourites_count":0,
- "protected":false,
- "profile_sidebar_fill_color":"e0ff92",
- "time_zone":null,
- "profile_sidebar_border_color":"87bc44",
- "profile_background_tile":false,
- "location":null,
- "id":18163247,
- "notifications":null,
- "statuses_count":67,
- "created_at":"Tue Dec 16 14:44:59 +0000 2008"
- },
- "truncated":false,
- "in_reply_to_status_id":null,
- "in_reply_to_user_id":null,
- "id":1731244194,
- "favorited":false,
- "source":"<a href=\"http:\/\/twitterfon.net\/\">TwitterFon<\/a>",
- "created_at":"Thu May 07 21:36:12 +0000 2009"
- }]
Look at all that great content! A quick glance will show that the object is an array of objects (notice the string begins and ends with square brackets). Therefore, if we assigned this string to a variable myTweets, we can access our first object with myTweets[0], for instance.
Here are some examples of accessing the data:
- myTweets[0].text // "Just handed in my last project ever for ..."
- myTweets[0].created_at // "Thu May 07 21:36:12 +0000 2009"
- myTweets[0].user.name // "franklakatos"
Step 2 - Access the Data Using Callbacks
The astute reader may have noticed that the object included in the API file has no variable assigned to it, and since we include the JSON object in an external javascript file, there is no way for us to assign one to it. So how are we suppose to access this data?
The answer, callbacks. Most APIs using JSON will implement a callback system, where you tell it a name of one of your functions and when your site is done downloading the JSON file, it will automatically call that function and pass the object as an argument.
Inside your function, you can assign it to a variable, use the data as needed, et cetera. All you need to do is create the function with one parameter, which will be used to pass the object along.
- var myTweets;
- function cb(data){
- myTweets = data;
- }
Now you can access the objects data whenever you would like, using the myTweets global variable. Here is an example of how you can implement the Twitter API to use in production:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
- <head>
- <script type="text/javascript">
- var myTweets
- function cb(data){
- myTweets = data;
- document.write(myTweets[0].user.name + " says:<br/>\"" + myTweets[0].text + "\"");
- }
- </script>
- <script type="text/javascript"
- src="http://twitter.com/statuses/user_timeline/franklakatos.json?callback=cb&count=1">
- </script>
- </head>
- <body>
- </body>
- </html>
...which would output..
- franklakatos says:
- "Just handed in my last project ever for my masters, all I have now is finals next week. Goodbye, school. Hello, life."
Next, we will get into the true power of using JSON: implementing client and server communication.
Introduction to JSON and PHP
Introduction to Computer Storage and Memory
Cross Browser CSS Opacity and the JavaScript Fade / Fading Effect