3

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?

3 Answers 3

3

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:
Sign up to request clarification or add additional context in comments.

2 Comments

this code is perfect! thanks so much!!! aother question... if i want to change its values will decodedObj[key] = "new value" will save the new value on the JSON object? thanks!
No, but you can at any time re-encode your object with JSON.encode(decodedObj);
0

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

Comments

0

I suggest you use Adobe's Native JSON decoding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.