vincent
vincent

Reputation: 197

getting data from imdb

i'm trying to get data from imdb to as3. I tried the following:

         var urlLoader:URLLoader = new URLLoader();
     urlLoader.addEventListener(Event.COMPLETE, urlCompleteHandler);
     urlLoader.load(new URLRequest("http://www.imdbapi.com/?t=The+Green+Mile "));

    private function urlCompleteHandler(e:Event):void {

        var resultXML:XML = new XML(e.target.data);
        trace(resultXML);
    }

Now if i trace resultXML i get:

{"Title":"The Green Mile","Year":"1999","Rated":"R","Released":"10 Dec 1999","Genre":"Crime, Drama, Fantasy, Mystery","Director":"Frank Darabont","Writer":"Stephen King, Frank Darabont","Actors":"Tom Hanks, Michael Clarke Duncan, David Morse, Bonnie Hunt","Plot":"The story about the lives of guards on death row leading up to the execution of black man accused of child murder & rape, who has the power of faith healing.","Poster":"http://ia.media-imdb.com/images/M/MV5BMTUxMzQyNjA5MF5BMl5BanBnXkFtZTYwOTU2NTY3._V1._SX320.jpg","Runtime":"3 hrs 9 mins","Rating":"8.4","Votes":"214654","ID":"tt0120689","Response":"True"}

now i'm trying to get the "Rating" from this result but i have no idea how to get it.

i tried: resultXML.Rating but that doesnt return any result.

Anyone knw how i can get the rating?

Upvotes: 1

Views: 835

Answers (1)

sberry
sberry

Reputation: 132018

That response is not XML, it is JSON. If you want XML add a r=xml to your query string.

package  {

    import flash.display.MovieClip;
    import flash.net.*;
    import flash.events.Event;


    public class IMDB extends MovieClip {

        var urlLoader:URLLoader;

        public function IMDB() {
            // constructor code

            urlLoader = new URLLoader();
            urlLoader.addEventListener(Event.COMPLETE, urlCompleteHandler);
            urlLoader.load(new URLRequest("http://www.imdbapi.com/?t=The+Green+Mile&r=xml"));
        }

    private function urlCompleteHandler(e:Event):void {   
            var resultXML:XML = new XML(e.target.data);
            trace(resultXML.movie.@rating);
        }
    }   
}

If you want to use JSON, then you will need a way to decode it. I suggest using as3corelib which is a library full of useful helper classes, including a JSON encoder / decoder.

If you used that, you would just do

var movieObj:Object = JSON.decode(e.target.data);
trace(movieObj.Rating);

which is similar to what you already have.

Upvotes: 4

Related Questions