I'm attempting to parse multiple JSON files into different objects.
Here is my approach:
function downloadDataSave (targetObject) {
// DOWNLOAD CALCULATION BACKUP
var filename, response;
filename = targetObject.name + '.json' ;
$.ajax
({
type: "POST",
url: datasaveHostname,
timeout : 3000 ,
data: {
action : "download", target : filename, data : ""
},
success: function (data) {
response = JSON.parse(data) ;
this[targetObject] = response ; // doesn't work , results in empty object
window[targetObject] = response ; // doesn't work , results in empty object (as it's the same)
resolutionHistory = response ; // does work, but how to solve this for more than one targetObject??
},
error : function (data) { console.log(targetObject.name + " : Download failed , ServerMessage : " + data); }
});
};
Please see the comments inside success. Functions like "console.log(response)" are returning the correct Object(s) inside resolutionHistory for example.
Any ideas?
derdigge
EDIT
This is how objects are created:
objects = [
"resolution", "resolution_history",
"roughsorting", "roughsorting_history",
"cleaning", "cleaning_history",
"fractionation", "fractionation_history",
"finalseparation", "finalseparation_history",
"thickening", "thickening_history"
];
function createObjects () {
for (var i = 0; i < objects.length; i++) {
window[objects[i]] = {};
window[objects[i]].name = objects[i] ;
}
};
EDIT2
I have added more console logs inside the code snippet:
Function downloadDataSave (targetObject, target) {
// DOWNLOAD CALCULATION BACKUP
var filename, response ;
console.log(this[aufloesungHistory]) ; //undefined
if (!(arguments[1])) {
filename = targetObject.name + '.json' ;
} else {
filename = arguments[1] + '.json' ;
}
$.ajax
({
type: "POST",
url: datasaveHostname,
timeout : 3000 ,
data: {
action : "download", target : filename, data : ""
},
success: function (data) {
response = JSON.parse(data) ;
console.log(aufloesungHistory) ; // empty object
this[targetObject] = {} ;
console.log(aufloesungHistory) ; // empty object
this[targetObject] = response ;
console.log(aufloesungHistory) ; // empty object
aufloesungHistory = {} ;
console.log(aufloesungHistory) ; // empty Object
aufloesungHistory = response ;
console.log(aufloesungHistory) ; // right contents inside object
console.log(this[aufloesungHistory]) ; // right contents inside object
console.log(this[targetObject]) ; // right contents inside object
},
error : function (data) { console.log(targetObject.name + " : Download failed , ServerMessage : " + data); }
});
};
The actual data looks like this inside a correct object: correct object
The .json file is created earlier from a correct object using JSON.stringify(targetObject)
in an upload function which has a similar structure.
Here is an example of a json blob:
{"name":"aufloesung_history","0":{"start":1446043200,"stop":1446063000,"start_h":"28.10.2015, 15:40","stop_h":"28.10.2015, 21:10","duration":19800},"1"... (truncated for brevity)
EDIT_3 (SOLUTION)
I found the solution! The issue was caused by "this" being an Object inside ajax. jQuery and my Brain led to that error. console.log(this)
inside the success section of ajax helped me understand. I used the key name (aufloesung.name
for instance) to populate a new object. This only occurs on the first load of the project so it should be okay. Please refer to the comments inside the code.