Parsing JSON returned by URLLoader - actionscript-3

I'm using a URLLoader and URLRequest to make a call to youtube's api. The return is formatted as json and looks like the following: http://pastebin.com/WxPS9NCB.
I'm trying to capture the "href" value located on line 42 in the above pastebin. But the code I have isn't working.
var urlLoader:URLLoader = new URLLoader(new URLRequest(apiURL));
urlLoader.addEventListener(Event.COMPLETE, function(e:Event) {
var json:Object = e.target.data;
var href:String = json.link[0].href;
trace(href);
});
Any ideas?

Flash does not parse JSON automatically. Use AS3 core libs JSON parser (https://github.com/mikechambers/as3corelib)
And replace var json:Object = e.target.data; with var json:Object = JSON.decode(e.target.data);
EDIT:
After a cursory glance at the JSON file you should use json.feed.link[0].href to access the data you are looking for.

try this:
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(new URLRequest(apiURL));
urlLoader.addEventListener(Event.COMPLETE, function(e:Event) {
var json:Object = e.target.data;
var href:String = json.link[0].href;
trace(href);
});

Related

How to parse JSON in AS3

I'm trying to build an application in Flash, but I have a problem. I'd like to parse the JSON from an web URL.
link: JSON text to parse
I'd like to get the JSON string "title" from here. Is there a way I can do it?
It says undefined.
var VidDataLoader = new URLLoader();
var VidUrl = "THEURL" + param1.getString(0);
trace(VidUrl)
VidDataLoader.load( new URLRequest(VidUrl))
VidDataLoader.addEventListener(Event.COMPLETE, doneit)
function doneit(e:Event){ var myData:Object = JSON.parse(VidDataLoader.data);
You are trying to reach the data from the VidDataLoader class, but the loaded data is part of the event object:
public function JSONLoader() {
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, decodeJSON);
loader.load(new URLRequest("myfile.json"));
}
// use the event to get the data
private function decodeJSON(e:Event):void {
var loader:URLLoader = URLLoader(e.target) ;
var jsonObject:Object = JSON.parse(loader.data);
}

Imdb api with flash - as3 - flex

I am trying to use the imdb API, in this case: http://imdbapi.org/
I need to search for a movie by name and get a json, then load an image with the poster obtained.
I'll be using as3 - flex to generate an Air package.
I tried this example, but can't seem to get it right.
import flash.net.*;
var url:String = "http://imdbapi.org/";
var request:URLRequest = new URLRequest(url);
request.method = URLRequestMethod.GET;
var variables:URLVariables = new URLVariables();
variables.name = "Pulp fiction";
request.data = variables;
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComplete);
loader.dataFormat = URLLoaderDataFormat.TEXT;
loader.load(request);
function onComplete (event:Event):void {
trace(event.target.data);
}
Perhaps you could enlight me with an example of connecting to the api and retrieving that json so that I can load an image with the poster and generate my air package.
Many thanks!
The API seems to want the movie in the q param so change this
variables.name = "Pulp fiction";
to :
variables.q = "Pulp fiction";
To verify : http://imdbapi.org/?q=Pulp%20Fiction
From there getting the poster URL is just a matter of reading the correct property from the JSON string.
private function onComplete (event:Event):void {
var data:Array = JSON.parse(event.target.data);
if(data && data.length)
{
var movie:Object = data[0];
trace(movie.poster);
}
}

JPEGencode issue in as3

Here is my code. What I m trying to achieve is to be able to capture an image from the the camera and upload it onto a media server but so far I have not been able to encode it succesfully .Can someone please point me in the right direction.
Here is the code
var imagePromise:MediaPromise = event.data;
imageLoader = new Loader();
imageLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, asyncImageLoaded );
imageLoader.addEventListener( IOErrorEvent.IO_ERROR, cameraError );
imageLoader.loadFilePromise( imagePromise );
function asyncImageLoaded(event:Event):void
{
var destination:String = "upload.php";
var now:Date = new Date();
var fileName = "IMG" + now.fullYear + now.month + ".jpg";
var image:Bitmap = Bitmap(imageLoader.content);
var bitmapData:BitmapData = image.bitmapData;
var j = new JPGEncoder(80);
var bytes:ByteArray = j.encode(bitmapData);
}
This is the error I get when i try to encode the image
TypeError: Error #1009: Cannot access a property or method of a null object reference.
which line? 1009 means you're trying to access a variable of something that is NULL. I'm guessing in this case, it's probably the line:
var image:Bitmap = Bitmap(imageLoader.content);
Try adding this:
if (imageLoader.content is Bitmap) {
var image:Bitmap = Bitmap(imageLoader.content);
} else {
throw new Error("What the heck bob?");
}
If it's an error, I bet the content was not decoded properly (which could mean a mime-type that wasn't image/jpg)
Additionally, you could probably use the native jpeg encoder for speed: (flash 11 i believe?)
var byteArray:ByteArray = new ByteArray();
bitmapData.encode(new Rectangle(0,0,640,480), new flash.display.JPEGEncoderOptions(), byteArray);
http://help.adobe.com/en_US/as3/dev/WS4768145595f94108-17913eb4136eaab51c7-8000.html

How do you read and write http headers in ActionScript?

Is it even possible to both read and write http headers in actionscript?
Hmm... ok setting them doesn't seem to be a problem.
Pretty straight forward, you just add and array of URLRequestHeader objects to the URLRequest object.
var req:URLRequest = new URLRequest();
var someheader:URLRequestHeader = new URLRequestHeader( "Connection", "OK" );
req.requestHeaders = [someheader];
However, you can't read them in the regular FlashPlayer. This can be done in AIR and Flash Lite on using the requestHeader property on the HTTPStatusEvent.
var loader:URLLoader = new URLLoader();
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onStatus);
private function onStatus( e:HTTPStatusEvent ) : void
{
var headers:Array = e.requestHeaders; // only available in FlashLite and AIR
}
This is kinda weird.

how to read the data from the JSP in a MXML through actionscript?

We are collecting the data at run time in Collection object in JSP page. We want to read this data from the JSP in a MXML through actionscript.
pls share the sample if you have.
thanks,
Sudarshan
function loadData():void
{
var ldr:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("page.jsp");
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(request);
}
private function onLoad(e:Event):void
{
var ldr:URLLoader = URLLoader(e.target);
trace(ldr.data);//traces the loaded string
//if the data is xml
/*
var myxml:XML = new XML(ldr.data);
trace(myxml.toXMLString());
*/
//update: answer to the comment:
//If the input string just lacks a root tag from being valid xml,
//you can introduce a dummy root tag.
var myxml:XML = new XML("<root>" + ldr.data + "</root>");
trace(myxml.data.toString()); //Hello
trace(myxml.value.toString()); //Hi
}
page.jsp should serialize the collection to appropriate format (xml/json/whatever) and return it.