Tutorial : Using JSON with Flex 2 and ActionScript 3
One of the little gems you will find in the open source ActionScript 3 libraries that we released on labs, is the JSON class found in the corelib library. This class, written for Adobe by Darron Schall, makes it super simple to both serialize and de-serialize JSON data. What is JSON you ask? From the JSON website: JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language. In a nutshell, JSON is a way to serialize data using JavaScript syntax. It is fairly compact, and easy to use. Furthermore, it has become increasingly popular due to the growth of AJAX applications, and the browser's native support of de-serializing JSON (via eval()).
If you look on the main page of my weblog, the Reading / Doing section on the left is actually a small AJAX widget I created that shows my latests diggs, delicious bookmarks, and items added to my ta-da lists. It gets its data from a JSON feed on the server, which is periodically generated by a PHP file. So, to show how to use JSON within a Flex / Flash application, we will load the JSON feed from my site into Flash and display it in a datagrid. Nothing earth shattering, but it will show how easy it is to use JSON. I am not assuming that you know anything about Flex / ActionScript or JSON. Because of this, the tutorial will be pretty long, as I want to make sure I cover every step of the process.
You can view the completed example here. (Requires Flash Player 8.5 beta 2). First, you need to make sure you have the following installed: You can grab Flex Builder and MXMLC from labs (the MXMLC compiler is included with the Flex Builder install, and also in the SDK download (in case you are on MAC or Linux). Once you download the zip file for the corelib library, unzip it and you should see three subfolders: src, bins, docs.- src : contains the source code for the library
- bin : contains a compiled SWC of the library (you can think of this as a DLL)
- docs : contains the API docs for the library
- Give the control an ID
- Set the layout constrains
- Top Right
- Top Left
- Left Top
- Left Bottom
Call to a possibly undefined method 'onJSONLoad'
This is ok. We just need to define the onJSONLoad event handler. But first, lets look at the code we just wrote:
- id - Give the control a variable name so we can reference it later.
- url - the url that points to the JSON data we are loading
- resultFormat - the format that we want the data returned to us in. (In this case, just the raw text).
- result - event handler that is called when the data loads.
- Get the raw JSON data from the URL
- De-serialize it from JSON to ActionScript
- Set the data as the dataProvider for the grid
- Actually tell the service to load the data.
- Customize the DataGrid to map to the data we are passing it.
- title
- src
- url
- date
Related Entries
- Log your AS3 corelib Library bugs / features
- Global Variables within Flex Applications
- How to log bugs for the AS3 Libraries
- Twitter AS3 API
- AsDoc (AS3 Doc Generating Tool) on Labs
Comments
-
Great tutorial Mike. I like that you took the time to explain every detail...makes it much easier for someone new to Flex (ie. me) to find my way around and figure out what's going on.
-
It is very usefull.
-
Very good Mike. I totally agree with Rick, having every step explained is a great help. More of it please, we're all hungry for Flex2.
-
NiluMike,
this is very nice attempt. I do have one question, is there a good way to do nested data structures and get a corresponding object. Something like.
{'EMPL':{'ID':1005,'Login_ID':'galileo','Name':'Galileo Galilei'}}
{'BOSS':{'ID':1005,'Login_ID':'galileo','Name':'Hudini Galilei'}} I would like to get a a JSON object which may have multiple arrays in it.
Thanks in advance!
Nilu.
P.S. We are *very* interested to get this Flex2 when ready. Can't wait.
-
AnonymousHi Mike, the new versions fo your libraries do not contain the swc files or the bin directory (as stated). Is this intentional?
-
>the new versions fo your libraries do not contain the swc files or the bin directory (as stated). Is this intentional? Oops. Sorry about that. I just uploaded an fixed version of corelib that includes the swc, and will fix the rest later today. Sorry about the hassle. mike chambers mesh@adobe.com
-
Thanks for this great tute. This is exactly the kind of thing needed to walk people through Flex Builder's workflow and showcase its features.
-
If you use this with FlexBuilder 2, you'll need to change com.macromedia.serialization.* to com.adobe.serialization.*
-
stefmike great tutorial, so many thanks. Q: could you give a brief overview of how you create the JCON file using PHP? i mean is it a simple act of calling something like JCON.encode(phpObject)? i take it the php object needs to represent the DataProvider class? THANKS AGAIN!!! STEF
-
DanMike, thanks for the posting on the webapis. I'm trying to work with the flickr api, and can't seem to get past the sandbox security. Flickr has a wildcard crossdomain.xml Loading that doesn't seem to work when viewing as localhost or on a server. Can you take a brief moment to give us an example of how to set up a flex app to allow flickr images to display? It looks like a lot of effort went into the webapis, so I think it would be beneficial to all, considering there's no tutorial on the flickr api.
-
Thanks a lot for the example. Give us more lease. One note for cut and pasters like me, the import com.macromedia.... should now be import com.adobe...
-
infimateI tried out the library. One thing i found was that the json decoder does not seem handle new lines and single quotes in json text. I would also like to know if it there is something like like they have for xml so that i could bind form elements to json. I can't seem to find it.
-
AlphonsoI'm very interested in Flex2 and would've liked to check your example, but the example ain't working. (I've the latest Flash Player on my machine). http://weblogs.macromedia.com/mesh/files/flex/jsonexample/ The screen in empty!

