What is JSON? JSON stands for JavaScript Object Notation and is a much leaner alternative to loading XML data. For more specific information be sure to check out the official JSON website. For this example we will be loading JSON data in from some del.icio.us posts. First you will need to download the JSON Class file here.

Open Flash and on the first frame and apply this code.

Note: The AS File that you downloaded above must be in the same directory as your Flash file.

import JSON;
var jsondata:LoadVars = new LoadVars();
jsondata.onLoad = function() {
var jsonObj = JSON.stringify(jsondata);

So now we need to understand exactly what we just did. The first line imports the AS file that you downloaded and put in the same directory as your Flash file. The next line creates a new LoadVars Object for us to begin communicating with a server (in this example the del.icio.us server). Now that we have the created a new LoadVars Object with the name of jsondata we can create an onLoad function which is called as soon as the loading of our data from the server has ended. Inside our onLoad function we create a new Object called jsonObj which is populated with the data that we receive from the server. JSON.stringify translates the del.icio.us Object structure into a string which we then convert back into an Object once in Flash. The next line simply traces our data to the output window so that we can detect if we have received it correctly. The last line simply loads the JSON data from the del.icio.us server.

This is a short introduction for loading JSON data into Flash. Many different web services are providing JSON feeds as an alternative to XML. A few of which can be seen here...

I hope you find this tutorial helpful and an easy way to how simple it can be to load JSON data into Flash. Also the corelib library for Actionscript 3 which can be found here includes several utilties for converting and working with JSON Objects in Flash even easier. As this technology continues to grow I believe we will see more and more support for using it in a variety of ways.


Lauren Smith