我正在尝试从一个对象加载多个 JSON 文件,以便我可以从其各自的对象中检索每组数据。因此,例如,对于下面的对象,我想获取所选特定对象的数据集。
//Key values are the file names and keys are what we use to select which data I wanna get that is stored in "servicesObj"
static var customServices = {EMOTE: "emote_frames", NPC: "npc"};
我开了一个课程来处理这个问题
class com.customs.Net.CustomServicesManager {
var customServices, servicesObj, serviceTypeArr, jsonLoader, serviceLoaderArr, serviceTypes, __get__serviceType, arrServicesToLoad, _loadingServiceType;
public function CustomServicesManager() {
super();
trace("Loaded Custom Services Manager");
servicesObj = {};
customServices = com.customs.Settings.customServices;
}
function getServiceData(type) {
var _local2 = servicesObj[type];
return (_local2);
}
public function get serviceType() {
serviceTypeArr = [];
for(var i in customServices) {
serviceTypeArr.push(i);
}
return (serviceTypeArr);
}
public function serviceTypeVal(val) {
for(var i in customServices) {
if(i === val) {
return (customServices[i]);
}
}
}
public function loadCustomServices() {
arrServicesToLoad = this.__get__serviceType();
jsonLoader = new com.cp.util.JSONLoader();
jsonLoader.addEventListener(com.cp.util.JSONLoader.COMPLETE, onCustomServicesJSONLoaded, this);
loadOngoingServices();
}
public function loadOngoingServices() {
if(arrServicesToLoad.length === 0) return (undefined);
_loadingServiceType = arrServicesToLoad;
for (var i = 0; i < _loadingServiceType.length; ++i) {
//for (var i in __get__serviceType()) {
serviceTypes = _loadingServiceType[i];
jsonLoader.load(serviceTypeVal(serviceTypes) + ".json");
}
}
public function onCustomServicesJSONLoaded(event) {
var _local2 = jsonLoader.data;
servicesObj[serviceTypes] = _local2;
trace(com.clubpenguin.util.JSONParser.stringify(servicesObj));
// trace(servicesObj["NPC"]["130_npc"].uniqueId);
// trace(servicesObj["EMOTE"].frame_menu_id);
trace(servicesObj["NPC"]["130_npc"].uniqueId);
}
}
每个 JSON 对象都应与
servicesObj
一起存储在 serviceType
中。这样,我就可以检索他们自己的数据集,如下所示:servicesObj["NPC"] ["130_npc"].uniqueId
,但由于某种原因,它只设置对象中的最后一个索引NPC,除非使用 NPC,否则我无法访问 EMOTE。
这些是存储所有内容的
servicesObj
的结果
{"NPC":[{"disabled":true,"emote_name":"Laughing Face","frame_menu_id":1,"frame_balloon_id":1},{"emote_name":"Smiley","frame_menu_id":2,"frame_balloon_id":2},{"emote_name":"Coffee Cup","frame_menu_id":3,"frame_balloon_id":13},{"emote_name":"Straight Face","frame_menu_id":4,"frame_balloon_id":3},{"emote_name":"Frown","frame_menu_id":5,"frame_balloon_id":4},{"emote_name":"Game","frame_menu_id":6,"frame_balloon_id":18},{"emote_name":"Surprise","frame_menu_id":7,"frame_balloon_id":5},{"emote_name":"Sticking Out Tongue","frame_menu_id":8,"frame_balloon_id":6},{"emote_name":"Popcorn","frame_menu_id":9,"frame_balloon_id":29},{"emote_name":"Wink","frame_menu_id":10,"frame_balloon_id":7},{"emote_name":"Green Sickly Face","frame_menu_id":11,"frame_balloon_id":8},{"emote_name":"Pizza","frame_menu_id":12,"frame_balloon_id":24},{"disabled":true,"emote_name":"Red Angry Face","frame_menu_id":13,"frame_balloon_id":9},{"emote_name":"Sad Face","frame_menu_id":14,"frame_balloon_id":10},{"emote_name":"Ice Cream","frame_menu_id":15,"frame_balloon_id":26},{"emote_name":"Meh Face","frame_menu_id":16,"frame_balloon_id":11},{"emote_name":"Cake","frame_menu_id":17,"frame_balloon_id":28},{"emote_name":"Clover","frame_menu_id":18,"frame_balloon_id":17},{"emote_name":"Heart","frame_menu_id":19,"frame_balloon_id":30},{"emote_name":"Lightbulb","frame_menu_id":20,"frame_balloon_id":12},{"emote_name":"Flower","frame_menu_id":21,"frame_balloon_id":16},{"emote_name":"Skull","frame_menu_id":22,"frame_balloon_id":40},{"emote_name":"Shocked Face","frame_menu_id":23,"frame_balloon_id":41},{"emote_name":"The Nigger","frame_menu_id":24,"frame_balloon_id":42},{"emote_name":"Pink Flower","frame_menu_id":25,"frame_balloon_id":43}]}
{"NPC":{"130_npc":{"isEnabled":true,"itemsObj":{"total_membership_days":0,"is_member":1,"frame":0,"y_corr":100,"x_corr":100,"photo_id":0,"flag_id":0,"feet":0,"hand":0,"body":0,"neck":0,"face":0,"head":0,"colour_id":0},"bitmask":1,"username":"[NPC] Pelican","uniqueId":9000251},"100_npc":{"isEnabled":true,"itemsObj":{"total_membership_days":0,"is_member":1,"frame":0,"y_corr":256,"x_corr":467,"photo_id":0,"flag_id":0,"feet":0,"hand":0,"body":467,"neck":256,"face":26,"head":0,"colour_id":8},"bitmask":1,"username":"[NPC] Bob","uniqueId":9000234}}}
第一部分需要是
EMOTE
不是NPC,但第二部分是正确的。
我不知道
jsonLoader
,也找不到任何相关信息,但有些事情看起来很奇怪
jsonLoader.addEventListener(com.cp.util.JSONLoader.COMPLETE, onCustomServicesJSONLoaded, this);
留下的印象是,
jsonLoader
异步工作,即您启动它,一段时间后,事件处理程序被调用。
那你就做吧
for (var i = 0; i < _loadingServiceType.length; ++i) {
serviceTypes = _loadingServiceType[i];
jsonLoader.load(serviceTypeVal(serviceTypes) + ".json");
}
即启动一堆并发加载操作。由于加载程序看起来是异步的,因此循环不会在开始下一个操作之前等待
load
操作完成,而是会快速遍历所有操作。不知道loader是否支持这个。但接下来是事件处理程序(我删除了所有日志记录和跟踪语句)
public function onCustomServicesJSONLoaded(event) {
var _local2 = jsonLoader.data;
servicesObj[serviceTypes] = _local2;
}
这里您访问的不是来自
event
的数据,而是 jsonLoader
本身的属性。我真的不明白这在异步环境中是如何工作的。也许加载程序在引发事件之前设置 this.data
,因此实际上可能为每个完成的加载设置正确的数据对象,然后您可以访问。
但更大的问题是,您正在使用
serviceTypes
变量来确定新数据块的密钥。但由于这个 serviceTypes
变量包含在类中,因此它只能有一个值。这个值会在 for
循环的每次迭代中更新,在循环中开始加载操作。由于这个循环早已完成,在第一个文件完成加载之前,serviceTypes
的值当然始终是它设置的最后一个值,即_loadingServiceType
数组中的最后一个值。
正如我所说,我不知道这个 JSONLoader 类,所以我不知道您可以从事件处理程序中的
event
收集哪些信息。但你得想办法,传递当前事件适用于哪个文件的信息。简单的计数器可能也不起作用,因为无法保证加载将以什么顺序完成。如果这是不可能的,您必须序列化您的数据读取,即在其他操作完成之前不要启动新的加载操作。