JSON in Action Script: how to get the properties and values on a JSON object?
suppose开发者_Python百科d 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?
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:
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
I suggest you use Adobe's Native JSON decoding.
精彩评论