我正在尝试从imdb获取数据到as3。我尝试了以下方法:
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);
}
现在,如果我跟踪resultXML,我得到:
{"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"}
现在我正试图从这个结果中获得“评级”,但我不知道如何获得它。
我试过:resultXML.Rating
但是这并没有返回任何结果。
任何人都知道如何获得评级?
答案 0 :(得分:4)
该响应不是XML,而是JSON。如果您想要XML,请在查询字符串中添加r=xml
。
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);
}
}
}
如果你想使用JSON,那么你需要一种方法来解码它。我建议使用as3corelib这是一个充满有用的辅助类的库,包括一个JSON编码器/解码器。
如果你使用它,你就可以
var movieObj:Object = JSON.decode(e.target.data);
trace(movieObj.Rating);
与您已有的相似。