Flash actionscript3从json数组获取随机元素
如何在action script中导入json文件,然后从json数组/对象中获取随机元素?由Mike Chambers下载,并使用包含的类将json字符串转换为ActionScript对象(在您的示例中是数组) 如果它是一个包含数组的JSON对象,则必须编写如下内容:Flash actionscript3从json数组获取随机元素,flash,apache-flex,actionscript-3,json,Flash,Apache Flex,Actionscript 3,Json,如何在action script中导入json文件,然后从json数组/对象中获取随机元素?由Mike Chambers下载,并使用包含的类将json字符串转换为ActionScript对象(在您的示例中是数组) 如果它是一个包含数组的JSON对象,则必须编写如下内容: var myObject:* = new JSONDecoder(myJsonObject, true).getValue(); var myArray:Array = myObject.theArray; public fu
var myObject:* = new JSONDecoder(myJsonObject, true).getValue();
var myArray:Array = myObject.theArray;
public function getData():void{
var _URLpath:String = "localdata.json";
jsonLoader = new JSONLoader(_URLpath);
jsonLoader.addEventListener("dataReady", collectJSONData);
jsonLoader.addEventListener("dataFailed", doJSONFailed);
}
private function collectJSONData(evt:Event):void
{
jsonObject = jsonLoader.returnJsonDecoded();
}
然后生成一个介于0和数组中最后一个索引之间的随机nr:
var randomIndex:int = Math.round(Math.random() * (myArray.length - 1));
并访问元素:
var myElement:* = myArray[randomIndex];
我正在使用as3corelib解析JSON。你可以在这里下载: 我把它包装到课堂上:
import com.adobe.serialization.json.JSON;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class JSONLoader extends EventDispatcher
{
private var loader:URLLoader = new URLLoader() ;
private var request:URLRequest = new URLRequest();
public var jsonDecoded:Object = new Object();
public function JSONLoader(DataURL:String):void
{
var JSONString:String = DataURL;
//trace("JSONLoader - JSONString = "+JSONString);
var urlRequest:URLRequest = new URLRequest(JSONString);
//var urlLoader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, decodeJSON);
loader.addEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler);
loader.load(urlRequest);
loader.addEventListener(Event.COMPLETE, decodeJSON) ;
}
public function decodeJSON(event:Event):void
{
trace("JSONLoader - Jason - "+event.target.data);
jsonDecoded = JSON.decode(event.target.data);
dispatchEvent (new Event("dataReady"));
removeListeners();
}
public function urlLoadErrorHandler(event:IOErrorEvent):void
{
trace("Jason - unable to load data");
dispatchEvent (new Event("dataFailed"));
removeListeners();
}
public function removeListeners():void
{
loader.removeEventListener(Event.COMPLETE, decodeJSON) ;
loader.removeEventListener(IOErrorEvent.IO_ERROR, urlLoadErrorHandler);
}
public function returnJsonDecoded():Object
{
trace("Jason jsonDecoded - "+jsonDecoded);
return jsonDecoded;
}
}
那么就这样称呼它:
var myObject:* = new JSONDecoder(myJsonObject, true).getValue();
var myArray:Array = myObject.theArray;
public function getData():void{
var _URLpath:String = "localdata.json";
jsonLoader = new JSONLoader(_URLpath);
jsonLoader.addEventListener("dataReady", collectJSONData);
jsonLoader.addEventListener("dataFailed", doJSONFailed);
}
private function collectJSONData(evt:Event):void
{
jsonObject = jsonLoader.returnJsonDecoded();
}
然后你有了jsonObject,你可以像其他文件一样使用like数组和随机选取项目。。@Benny7500的答案解释了如何从服务器加载它。我有一个错误:“调用一个可能未定义的方法JSONDecoder。”您是否将AS3CoreLib库放在构建路径中?是否包含导入语句:
import com.adobe.serialization.json.JSONDecoder代码>只需扩展EventDispatcher
,因为绝对没有理由扩展Sprite