如何解析JSON(AS3)

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

3
可以使用JSON.parse()方法。详见http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/JSON.html。请提供更多问题信息。 - Azzy Elvul
1
添加代码。你尝试过什么?你搜索过什么?这个网站不是让别人为你编写代码的地方。欢迎来到 Stack Overflow =) - blue112
3个回答

19

好的,这里是来自我的当前项目的完整工作示例:

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

3
使用as3corelib解析JSON的函数(即非原生JSON类)是“decode()”。
JSON.decode( inputJson );

如果输入的 JSON 被正确编码,那么字符串应该可以在生成的对象中使用。如果字符串没有被正确转义,您可能会遇到解析字符串的问题,但这是输入数据的问题。

5
JSON现在已内置于Flash Player的默认软件包中,称为[JSON.parse()],不再需要as3corelib。此外,Flash Player运行时处理JSON比as3corelib的实现性能更高。 - Jason Sturges
@Jason Sturges - 我同意,原生的JSON解析器应该是首选。原始问题提到了as3corelib.swc,所以我指的是那段代码。 - null

0

在Adobe Animate中的ActionScript 3.0,

var obj:Object = [
    {
        "capital":"Santiago",
        "corregimientos":[
            {
                "santiago": "Capital of the province of Veraguas.",
                "habitantes":"It has an approximate population of 50,877 inhabitants (2014).",
                "area": "44.2 km²",
                "limits": "It limits to the north with the district of San Francisco, to the south with the district of Montijo, to the east with the district of Atalaya and to the west with the district of La Mesa."
            }
        ]
    }
];

trace("Capital " + obj[0].capital+" Corrections: "+obj[0].corregimientos[0].santiago);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接