0

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?

bruno
  • 32,421
  • 7
  • 25
  • 37
sayou
  • 893
  • 8
  • 29
  • Make `Employee` class accept an object from your response as a constructor argument, then build the object. Something like `let employee = new Employee(data.employees[0])` – Isaac Vidrine Mar 13 '19 at 18:07
  • @IsaacVidrine I assume `Employes` was chosen arbitrarily and can stand in for any string? Maybe OP can confirm. – ggorlen Mar 13 '19 at 18:09
  • Do you want them to just have the same properties as `employee` object or to actually **be** an employee object? – Isaac Vidrine Mar 13 '19 at 18:12
  • In standard OOP language (not in javascript terminology), are you trying to generate a new class based on the JSON data (probably because you don't know what the classes will be called), or are you just trying to instantiate objects of various pre-defined classes on the fly? Either is relatively easy in Javascript -- which, by the way, does NOT have classes (except as syntactic sugar), -- but the algorithms are going to be pretty different. – Cat Mar 13 '19 at 18:14
  • @Cat I want to generate new classes based on the JSON data, for example in my code I have Employes and infos on json, so I want to create Employes and infos classes automatically on my js code – sayou Mar 14 '19 at 07:27
  • There is no point in doing this. A class is really just a convenient way to create objects. You already have the objects, so no need for a class. – Felix Kling Mar 14 '19 at 20:35

1 Answers1

1

JS does not natively have classes in the same sense as most OOP languages. You probably come from an OOP background, you should learn about JS's prototype based inheritance.

You will understand that you don't actually have to create those classes. You can achieve what (I think) you are looking for via simply writing:

fetchJSONFile(function(data){ 
        employes = data.Employes; 
        // or, if you wish to make a copy
        //employes = data.Employes.map(e => {...e})
        console.log(employes);
    })
Marcell Toth
  • 3,433
  • 1
  • 21
  • 36
  • The `Employes` and `Infos` classes may have methods defined. – Felix Kling Mar 13 '19 at 18:19
  • As the OP said they would like to generate those classes automatically, I assume that there are none. I might have misunderstood the question though – Marcell Toth Mar 14 '19 at 14:53
  • Mmh I see. I thought given that they are calling the `Employes` constructor it already exists and they just don’t want to reference it explicitly. But after re-reading the question I think you are right. We should get clarification from the OP either way. – Felix Kling Mar 14 '19 at 15:10