actionscript 3 and JSON

 ̄綄美尐妖づ 提交于 2020-01-11 00:46:18

问题


I've been trying to get JSON working with AS3 for a while now, but to no avail. I keep getting the following error when I get the JSON back:

TypeError: Error #1034: Type Coercion failed: cannot convert Object@26331c41 to Array.

I've tried changing the datatype of the variable "jsonData" to object, which fixes the error, but I'm not entirely sure how I can parse the data.

package 
{
    import flash.display.Sprite;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.events.*;
    import com.adobe.serialization.json.JSON; 

    public class DataGrab extends Sprite {

        public function DataGrab() {

        }

        public function init(resource:String):void {
            var loader:URLLoader = new URLLoader();
            var request:URLRequest = new URLRequest(resource);
            loader.addEventListener(Event.COMPLETE, onComplete);
            loader.load(request);
        }   

        private function onComplete(e:Event):void {
            var loader:URLLoader = URLLoader(e.target);
            var jsonData:Array = JSON.decode(loader.data);
            trace(jsonData);
        }


    }
}

回答1:


You were correct when you had the jsonData variable as an Object. To iterate through all the properties of that variable you could just do something like this:

var jsonData:Object = JSON.decode(loader.data);
for (var i:String in jsonData)
{
    trace(i + ": " + jsonData[i]);
}

If you wanted to check if the object contained a specific property you could use something like:

var hasFooProperty:Boolean = jsonData.hasOwnProperty("fooProperty");


来源:https://stackoverflow.com/questions/1653514/actionscript-3-and-json

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