I have a JSON file which contains information data, and I get this data using AJAX using this function to fetch it.
function fetchJSONFile(callback) {
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4) {
if (httpRequest.status === 200) {
var data = JSON.parse(httpRequest.responseText);
if (callback) callback(data);
}
}
};
httpRequest.open('GET', 'database.json');
httpRequest.send();
}
And I fetch it to assign it to the object of the current Class:
fetchJSONFile(function(data){
employes = Object.assign(new Employes(), ...data.Employes);
console.log(employes);
})
The data within my JSON file is this:
{
"Employes":[
{
"id": 1,
"fullName": "Test Test"
}
],
"Infos":[
{
"id": 1,
"address": "Test Test test test test",
"employes": 1
}
]
}
My question is how to generate Classes automatically of each object type from the JSON file.
Using my code like for example I want to generate Employes
and Infos
Classes automatically here without having to create them manually.
fetchJSONFile(function(data){
employes = Object.assign(new Employes(), ...data.Employes); //Generate Classes automatically here from data type
console.log(employes);
})
Is there any solution?