I'm trying to load multiple JSON files from an object so that I can just retrieve each set of data from its own respective object. So, for example, for the object below, I want to get the set of data for that particular object selected.
//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"};
I made a class to handle this
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);
}
}
Each JSON object should be stored in servicesObj
along with the serviceType
. That way, I can retrieve their own set of data, like this: servicesObj["NPC"] ["130_npc"].uniqueId
, but for some reason it only sets the last index in the object NPC, and I can't access EMOTE unless using NPC.
These are the results from servicesObj
where everything is stored
{"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}}}
First part needs to be EMOTE
not NPC but the 2nd part is right.
I don't know that jsonLoader
and couldn't find any information about it, but a few things seem strange
jsonLoader.addEventListener(com.cp.util.JSONLoader.COMPLETE, onCustomServicesJSONLoaded, this);
leaves the impression, that the jsonLoader
works asynchronous, ie you start it, and some time later, the eventhandler is called.
Then you do
for (var i = 0; i < _loadingServiceType.length; ++i) {
serviceTypes = _loadingServiceType[i];
jsonLoader.load(serviceTypeVal(serviceTypes) + ".json");
}
ie start a bunch of concurrent loading operations. As the loader seems asynchronous, the loop isn't waiting for the load
operation to finish, before it starts the next one, but just rushes through them all. Don't know if the loader supports this, but let's assume it does. And then comes the eventhandler (I removed all the logging and tracing statements)
public function onCustomServicesJSONLoaded(event) {
var _local2 = jsonLoader.data;
servicesObj[serviceTypes] = _local2;
}
Here you are not accessing the data from the event
, but the property jsonLoader.data
(ie a property of the single jsonLoader
instance) itself. I really don't get how that would work in an asynchronous environment. Maybe the loader sets this.data
before raising the event so this might in fact for each finished loading set the correct data object, you can then access.
But the bigger problem is, you are using the serviceTypes
variable to determine the key for the new data block. But as this serviceTypes
variable is contained in the class it can only have a single value. And this value is updated in each iteration of the for
loop, where you start the load operations. And as this loop is long finished, before the first file has finished loading, the value of serviceTypes
is of course always the last value it was set to, ie the last value in the _loadingServiceType
array.
As I said, I don't know this JSONLoader class, so I don't know what information you can gather from the event
in the eventhandler. But you have to find a way to pass the information about which file the current event applies to. If that is not possible, you have to serialize your data reading, ie don't start a new load operation before the current one is finished.
If neither of those possibilities work, can you change the structure of the json you are loading? So you can include the name of the key in the json file. Something like
{
"key": "NPC",
"data": {
"130_npc":{ ...}
}
}
Then in your eventhandler do
public function onCustomServicesJSONLoaded(event) {
var key = jsonLoader.data.key;
var data = jsonLoader.data.data;
servicesObj[key] = data;
}