In my ng2 implementation, I have a user.service.ts file that calls a REST service and returns JSON data. The code snippet below shows how the getUser function retrieves the user information:
getUser(id: number): Promise<User> {
return this.http.get('http://localhost:4000/users/1')
.toPromise()
.then(response => response.json())
}
The JSON object returned from the service has the following structure:
{
"Id":"1"
"FirstName":"John"
"LastName":"Smith"
}
I want to convert this JSON object into an instance of my ng2 User entity, which is defined as follows:
export class User
{
Id: number;
FirstName: string;
LastName: string;
}
I am looking for a generic way to map the userResponse to the User entity without writing explicit mapping code each time. Ideally, I would like to automate this process using reflection or similar dynamic techniques. Is there a recommended approach to achieve this in ng2?