动作脚本 3 和 JSON [英] actionscript 3 and JSON

查看:23
本文介绍了动作脚本 3 和 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直试图让 JSON 与 AS3 一起工作,但无济于事.当我取回 JSON 时,我不断收到以下错误:

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:

类型错误:错误 #1034:类型强制失败:无法将 Object@26331c41 转换为数组.

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

我已尝试将变量jsonData"的数据类型更改为 object,从而修复了错误,但我不完全确定如何解析数据.

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);
        }


    }
}

推荐答案

当您将 jsonData 变量作为 Object 时,您是对的.要遍历该变量的所有属性,您可以执行以下操作:

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");

这篇关于动作脚本 3 和 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆