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");
精彩评论