动作3和JSON动作、JSON

2023-09-08 12:12:59 作者:原谅我的、爱

我一直在试图让JSON正与AS3有一段时间了,但无济于事。我不断收到以下错误,当我得到的JSON回来:

类型错误:错误#1034:类型强制失败:无法转换对象@ 26331c41到数组

我试图改变变量jsonData对象,它修正了错误的数据类型,但我不完全知道我怎么能分析数据。

 包
{
    进口flash.display.Sprite;
    进口flash.net.URLRequest;
    进口flash.net.URLLoader;
    进口flash.events *。
    进口com.adobe.serialization.json.JSON;

    公共类DataGrab扩展Sprite {

    公共职能DataGrab(){

    }

    公共职能的init(资源:字符串):无效{
    VAR装载机:的URLLoader =新的URLLoader();
    VAR要求:的URLRequest =新的URLRequest(资源);
    loader.addEventListener(引发Event.COMPLETE,的onComplete);
    loader.load(要求);
    }

    私有函数的onComplete(五:事件):无效{
    VAR装载机:的URLLoader =的URLLoader(e.target);
    VAR jsonData:阵列= JSON.de code(loader.data);
    跟踪(jsonData);
    }


    }
}
 

解决方案

当你有 jsonData 变量作为对象你是正确的。遍历该变量,你可能只是做这样的事情的所有属性:

  VAR jsonData:对象= JSON.de code(loader.data);
对于(VAR我:字符串中jsonData)
{
    迹第(i +:+ jsonData [I]);
}
 
3.JSON文件操作

如果你想检查对象包含您可以使用类似的特定属性:

  VAR hasFooProperty:布尔= jsonData.hasOwnProperty(fooProperty);
 

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


    }
}

解决方案

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