本文介绍了如何解析 JSON (AS3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将下载的 .json 文件解析为字符串变量?使用 as3corelib.swc.

How to parse downloaded .json file with a string inside it to a string variable? With as3corelib.swc.

推荐答案

我们开始吧,我当前项目的完整示例:

And here we go, full-working example from my current project:

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/
}

这篇关于如何解析 JSON (AS3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 00:26