Chinchan Zu
Chinchan Zu

Reputation: 918

JSON in Action Script: how to get the properties and values on a JSON object?

supposed i have this JSON, and supposed the properties may change...

'{"srcLocation":"pc","filename":"name","fileext":"jpg","url":""}';

first I want to get all the properties on them (srcLocation, filename etc) and using the properties I got I want to get the corresponding value..

how can I do that in action script?

Upvotes: 3

Views: 4320

Answers (3)

qwerber
qwerber

Reputation: 139

I suggest you use Adobe's Native JSON decoding.

Upvotes: 0

Swati Singh
Swati Singh

Reputation: 1863

First you should load the JSON file using URLLoader and then decode the data into Object and after that you will be able to get the corresponding value of the properties of that Object:

        var myRequest:URLRequest = new URLRequest("Your JSON file Name");
        var myLoader = new URLLoader();
        myLoader.addEventListener(Event.COMPLETE, onload);
        myLoader.load(myRequest);

        function onload(evt:Event):void
        {
            var myData:Object = JSON.decode(myLoader.data);
            trace(myData.firstName);
            trace(myData.job);
            trace(myData.age);              
        }

for any further help go through this link: http://swati61.blogspot.com/2011/06/json-and-as3-communication.html

Upvotes: 0

shanethehat
shanethehat

Reputation: 15580

First of all you need to decode the JSON, probably using as3corelib:

var jsonString:String = '{"srcLocation":"pc","filename":"name","fileext":"jpg","url":""}';
var decodedObj:Object = JSON.decode(jsonString);

Then all you have to do is loop through the object to get all the name/value pairs:

for(var key:String in decodedObj) {
    trace("Name: " + key + " - Value: " + decodedObj[key];
}

Which will output:

Name: srcLocation - Value: pc
Name: filename - Value: name
Name: fileext - Value: jpg
Name: url - Value:

Upvotes: 3

Related Questions