Loading JSON in ActionScript 2

Because I blew a few hours trying to figure this out the last few days, and finally cracking it, here’s how to fetch some JSON from a web service and have it processed into an object in ActionScript 2.

import JSON; // http://www.theorganization.net/work/jos/JSON.as

var jsonFetcher:LoadVars = new LoadVars();
jsonFetcher.onLoad = function(success:Boolean) {
	if (!success) {
		trace("Error connecting to server.");
	}
};
jsonFetcher.onData = function(thedata) {

	// ...
	// if writing a desktop app or widget, string manip or regexp
	// the JSON data packet out of the JS source provided by many
	// web services at this point..
	// ...

	try {
		var o:Object = JSON.parse(thedata);
		//mytrace(print_r(o));
		trace(print_a(o));
	} catch (ex) {
		trace(ex.name+":"+ex.message+":"+ex.at+":"+ex.text);
	}
};

// get the feed
jsonFetcher.load("url-to-json-feed.php");

// from: http://textsnippets.com/posts/show/633
// recursive function to print out the contents of an array similar to the PHP print_r() function
//
function print_a(obj, indent) {
	if (indent == null) {
		indent = "";
	}
	var out = "";
	for (item in obj) {
		if (typeof (obj[item]) == "object") {
			out += indent+"["+item+"] => Objectn";
		} else {
			out += indent+"["+item+"] => "+obj[item]+"n";
		}
		out += print_a(obj[item], indent+"   ");
	}
	return out;
}

BTW, what’s up with these JSON feeds that serve up JS that’s JSON plus some more code? That’s annoying — you gotta strip it out before you run the JSON converter :P.

Update: here’s the actionscript 2.0 JSON.as file people were looking for.

19 thoughts on “Loading JSON in ActionScript 2

  1. Boxey

    I have been trying to get the as2 json file for a while but the file is missing off the server. I dont suppose you could email it over could you.

    I have been trying this address:
    http://www.theorganization.net/work/jos/JSON.as

    Thanks

  2. You can get that AS file here:
    http://inner.geek.nz/uploads/JSON.as

  3. Brett thanks for help it works at my site.

    Keep up the good work.

    Greetings

  4. Fantastic code. Work great on my site. Again thanks and greetings

  5. Alex

    Can you please provide the JSON File you are using as an example?

  6. james

    the script causing the flash player run slowly… please provide the working as and fla file.

  7. Thank yoy it was really helpful especially for my boyfriend because he’s been looking for stuff like that for weeks!

  8. Thank you so much! I’ve been trying to figure this out for the past 2 days. The original class files from json.org and examples I found online were terrible. I was able to load and stringify, but it gave me no solution to extrapolate the values. With your example, I was easily able to just convert the whole thing into an array. Thanks again.

  9. absolutely awesome. I just spent 3 hours looking for exactly the code you got here, all other code examples out there were crap

  10. AshTemple

    thanks for this! ideal!!

  11. Martin

    Very, very, VERY helpful – finally I’m able to process json-formatted data.

    Thanks!

  12. Jacques

    Hi, this code is awesome and works 100% when I’m in flash. As soon as I place it on a live site it timesout 🙁 By process of elimination I’ve figured that the problem steps in on the “.onData” function.

    My swf embed are set to params.allowscriptaccess = “always”;
    I really don’t know what could be the problem.

    Does someone have any idea?

  13. brad

    Hey! Thanks for the code…. I’d suggest just showing some sample data though, instead of no data…. as I tried to use this and got “undefined” results, and burned about 30 min before I realized the problem was the print_a function not working well with my JSON data…. I incorrectly assumed the problem was with the JSON.as file and looked around for newer revs! Thanks so much for the post!.

  14. I didn’t write this library, just found the only existing copy of it.

  15. Nathan Scovell

    Hello, I am trying to learn how to use this with actionscript 2 and address the data to movieclips. Could you email me an example of using JSON with movieclips in flash.

    Nathan

  16. Sorry, I haven’t touched AS2 in many years for good reason, namely AS3 is much better. This post is from 2007 and really should stay there 🙂 Also, Flash is dying if you haven’t heard.

  17. Hello
    I need to use json in as2 but none of your class download links are working.

  18. Charlie

    For those looking for the JSON.as file, I found it here:

    http://stackoverflow.com/questions/4331042/problem-using-json-as

    🙂

Comments are closed.