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

怎甘沉沦 提交于 2020-01-13 04:49:07

问题


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?


回答1:


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:



回答2:


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




回答3:


I suggest you use Adobe's Native JSON decoding.



来源:https://stackoverflow.com/questions/6967118/json-in-action-script-how-to-get-the-properties-and-values-on-a-json-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!