Loading JSON in ActionScript 2

Filed under: Coding,Work — Brett @ January 18, 2007 9:43 pm

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.

17 Comments »

  • 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

    #1: Comment by Boxey — January 24, 2007 @ 2:17 am

  • I have an error with this file (http://www.theorganization.net/work/jos/JSON.as)

    #2: Comment by Tanie linie lotnicze — January 26, 2007 @ 10:51 pm

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

    #3: Comment by Brett
    Twitter:
    — January 30, 2007 @ 4:27 pm

  • Brett thanks for help it works at my site.

    Keep up the good work.

    Greetings

    #4: Comment by Pozycjonowanie — January 31, 2007 @ 1:50 am

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

    #5: Comment by Meble — February 10, 2007 @ 2:52 am

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

    #6: Comment by Alex — February 15, 2007 @ 6:40 am

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

    #7: Comment by james — May 1, 2007 @ 2:32 am

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

    #8: Comment by Tanie tĹ‚umaczenia — October 2, 2007 @ 5:15 am

  • 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: Comment by Jeff — December 19, 2008 @ 9:01 am

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

    #10: Comment by Robert — August 14, 2009 @ 8:36 am

  • thanks for this! ideal!!

    #11: Comment by AshTemple — January 19, 2011 @ 11:54 pm

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

    Thanks!

    #12: Comment by Martin — August 22, 2011 @ 4:20 am

  • 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: Comment by Jacques — August 29, 2011 @ 8:45 pm

  • 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: Comment by brad — January 5, 2012 @ 3:18 am

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

    #15: Comment by Brett
    Twitter:
    — January 6, 2012 @ 11:29 am

  • 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: Comment by Nathan Scovell — February 2, 2012 @ 12:26 pm

  • 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: Comment by Brett
    Twitter:
    — February 2, 2012 @ 2:42 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment