JSON Communication with Flash: Loading Data

JSON Communication with Flash: Loading Data

Lauren Smith

Explorin Lauren is in fact the moniker for Lauren Smith. I am a Dallas native and a life-long (read: fair-weather) Cowboys fan. I enjoy being outside, when I am not working, and I love a strong cup of coffee, especially while working. I am also a full-time Interactive Developer for LevelTen Design based out of Dallas, TX. I spend most of my days deeply buried in work on Actionscript, CSS, XHTML, or the occasional XSLT and PHP.
View all articles by Lauren SmithOpen 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);
trace(jsonObj)
};
jsondata.load("http://del.icio.us/feeds/json/explorinlauren");
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...
Yahoo!
Del.icio.us
Flickr
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.
Thanks.
Lauren Smith
www.explorinlauren.com
Spread The Word
Related Articles
11 Responses to "JSON Communication with Flash: Loading Data" 
said this on 18 Jul 2007 4:22:29 PM CDT
When i do this, i recieve
**Error** var js Can |
said this on 24 Feb 2010 9:55:31 AM CDT
The reason why you get th
import JSON; // important!!! var m va jsondata.on var trace(jsonO }; jsondata.load("h |
said this on 01 Aug 2007 7:48:41 PM CDT
Great article. I'm no
|
said this on 08 Oct 2007 4:09:53 PM CDT
Thats is cause JSON dosnt
Do this: then: json.s thats all |
said this on 21 Feb 2008 9:43:40 AM CDT
um the link to json.as is
|
said this on 09 Sep 2008 1:12:13 PM CDT
This works...
import c va var jsondata:LoadVars var j jsondata.load jsondata.on tra var trace(json } |
said this on 18 Feb 2010 10:27:10 AM CDT
say i extracted a json fi
|
said this on 04 Mar 2010 3:52:34 AM CDT
Hi,
I want to send dat How to send |
said this on 02 Mar 2011 8:26:52 AM CDT
I can't loaded json file
No The requested page link is http:/ |
said this on 18 Apr 2011 3:51:15 AM CDT
http://inner.geek.nz/uplo
|
said this on 08 Mar 2011 10:37:12 PM CDT
Same here link doesn't wo
|