How to parse JSON (AS3)
Asked Answered
F

3

9

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

Forgot answered 11/10, 2012 at 13:20 Comment(2)
use JSON.parse() help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/… Can you provide more info about the problem?Appropriation
Add code. What have you tried ? What have you searched for ? This website is not about other people doing the work and give you code. Welcome to stack overflow, anyway =)Hezekiah
A
19

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/
}
Aggrandize answered 12/10, 2012 at 13:58 Comment(0)
M
3

The function for parsing JSON using as3corelib ( ie not the native JSON class ) is 'decode()'

JSON.decode( inputJson );

If the input json is properly encoded, strings should be available inside the resulting object. You may have trouble parsing strings if they have not been correctly escaped, but that is a problem with the input data.

Misappropriate answered 12/10, 2012 at 2:59 Comment(2)
JSON is now built in to Flash Player default package called as JSON.parse() - as3corelib is no longer needed. As well, Flash Player runtime handling is JSON is higher performance than as3corelib's implementation.Doable
@Jason Sturges - agreed, the native JSON parser should be the preferred choice. The original question referred to as3corelib.swc, so I was referring to that code.Misappropriate
R
0

actionscript 3.0 in Adobe Animate,

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);
Ruling answered 12/6, 2021 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.