A while ago, the as3corelib has been published on Google Code with a useful tool: JSON encoder and decoder.
JSON (JavaScript Object Notation) is a lightweight computer data interchange format (that is intended to solve XML structure weight), that allows you to send Object (Array, Strings, Number, …) to your server scripts or to JavaScript.
[adsenseyu1]
JSON is commonly used by Yahoo Web Services for example.
To use JSON and URLRequest even faster and simpler, I’ve create a small class that allows you to configure your URLRequest in one line (called PhpInteract), to add variables and to precise if your variables are encoded in JSON.
[adsenseyu1]
AS3 example to load a XHTML file:
var mi:PhpInteract = new PhpInteract(); //New object of the PhpInteract Class mi.loadFile("http://www.w3.org/TR/2008/WD-xhtml-access-20080107/");
Now to get the content of the file just do as usual with the onComplete Listener:
mi.addEventListener(Event.COMPLETE, completeHandler); public function completeHandler(e:Event):void { //file is completely loaded trace("File Content :n"+mi.content); }
You are certainly thinking that a new Class to do this is completely useless… And I agree. Now we can do something more interesting and pass variables (encoded in JSON) to our server side script:
var arr:Array = new Array("a", "b", "c");//An array for the demo... mi.loadFile("http://yourserver/fromFlash.php",arr, true); mi.isJSON = true; //We specify that the content must be JSON encoded
Now for the demo, our below PHP code will decode the received JSON string and will re-encode it in JSON and echo it. Our Flash / Flex / AIR application will be able to decode the content as well and handle complex data from PHP (or any server side languages that can encode and decode JSON).
< ?php $flash = $_POST['fromFlash']; $testArray = json_decode($flash); //We can access to the Array Element echo "Array [0]".$testArray[0]."n"; echo "Array [1]".$testArray[1]."n"; echo "Array [2]".$testArray[2]."n"; echo json_encode($testArray); ?>
What you need to make it work:
– The as3corelib
– The PhpInteract Class
[adsenseyu1]
Below is a complete sample:
package { import flash.display.Sprite; import ch.metah.mURL.PhpInteract; import flash.events.*; public class InitmURL extends Sprite { var mi:PhpInteract = new PhpInteract(); public function InitmURL() { //var arr:Array = new Array("a", "b", "c");//for the demo //mi.loadFile("http://metah.ch/seo/fromFlash.php",arr, true); //mi.isJSON = true; //is JSON content in loaded file? mi.loadFile("http://www.w3.org/TR/2008/WD-xhtml-access-20080107/"); //USE EventListener mi.addEventListener(Event.COMPLETE, completeHandler); mi.addEventListener(Event.OPEN, openHandler); mi.addEventListener(ProgressEvent.PROGRESS, progressHandler); mi.addEventListener(HTTPStatusEvent.HTTP_STATUS, HTTPHandler); mi.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errorHandler); } public function completeHandler(e:Event):void { //file is completely loaded trace("File Content :n"+mi.content); } public function openHandler(e:Event):void { //trace("Download started"); } public function progressHandler(e:ProgressEvent):void { //Handle Progress //trace("Loaded"+mi.bytesLoaded+"Total"+mi.totalBytes); } public function HTTPHandler(e:HTTPStatusEvent):void { //trace("HTTP Status:"+mi.httpStatus); } public function errorHandler(e:SecurityErrorEvent):void { //Handle Security Error Here } } }
Ahmet
Hello, using this class for a flicker feed, output that comes back mi.content looks like:
jsonFlickrFeed({
“title”: “”,
“link”: “http://www.flickr.com/news/”,
“description”: “The latest Flickr news”,
“modified”: “2007-05-07T21:15:00Z”,
“generator”: “http://www.flickr.com/”,
“items”: [
{
“title”: “7 May ’07, 2.15pm PDT”,
“link”: “http://www.flickr.com/news/#1809”,
“media”: {“m”:””},
“date_taken”: “”,
“description”: “We’ve improved the Slideshow. Read more about it FlickrBlog.”,
“published”: “2007-05-07T21:15:00Z”,
“author”: “nobody@flickr.com (The Flickr Team)”,
“author_id”: “”,
“tags”: “”
}]
})
if I want to, say, have a text box that has a list of all the titles, and one that lists all the descriptions; how would I parse the mi.content object to achieve that?
can you help?
Hello,
I realy need some help on how to import content from a json file into flash with AS3 using corelib. there is not that much resource online about this the example you have posted now is not realy what I am looking for but I would think that you may know how to do this. could you please provide me examples or links to of how doing this.
Thanks
Hello Erick,
it is exactly what the Class PHPInteract do, download it and read the content you should find the solution. If not please be more explicit đŸ™‚
Hello Ahmet,
You seem to know JSON and AS3 quite well. I’m trying to use JSON data in a Flex project, and am currently stuck. Here is the JSON example data:
[{
“tracklisting”:
[{“artist”:”epmd”,”title”:”strictly business”},
{“artist”:”wu tang”,”title”:”Worldwide”},
{“artist”:”De la Soul”,”title”:”Say no go”},
{“artist”:”A tribe”,”title”:”find a way”}]
}]
And I’m using corelib, but don’t know how the syntax to access the artist values within the tracklisting array. At the moment I’m trying (but getting no result):
private function getArtistName (item:Object):String
{
return item.tracklisting.artist;
}
hi, I just try to test it and it’s not working I have error sayin that JSONType is not a constant of complilation… , I you could explain the process and why I get error would be good thanks
ok, I was loading two things at same time, sorry… I still get an other error.. but I will try harder and maybe comeback