如何将下载的.json文件(其中包含字符串)解析为字符串变量?使用as3corelib.swc。
最佳答案
接下来是我当前项目中的完整示例:
protected function loadConfigFromUrl():void
{
var urlRequest:URLRequest = new URLRequest(CONFIG_URL);
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
try{
urlLoader.load(urlRequest);
} catch (error:Error) {
trace("Cannot load : " + error.message);
}
}
private static function completeHandler(event:Event):void {
var loader:URLLoader = URLLoader(event.target);
trace("completeHandler: " + loader.data);
var data:Object = JSON.parse(loader.data);
trace("The answer is " + data.id+" ; "+data.first_var+" ; "+data.second_var);
//All fields from JSON are accessible by theit property names here/
}