Object Type and HTTP JSON Gets
-
I've been struggling with getting an Angular 6 app to sort the results of an WebAPI call, and it turns out that the root cause is case sensitivity on the object properties. And I can see 'where' the differing cases come from, but I can't see the where/why. I have a class for my object:
export class App
{
id: number;
name: string;
description: string;
isEnabled: boolean;
shortName: string;
displayOrder: number;
}And my web service returns JSON like:
{"ID":1
,"Name":"Matterhorn User Management"
,"Description":"User and Role management for Matterhorn applications."
,"IsEnabled":true
,"ShortName":"MUM"
,"DisplayOrder":10}To retrieve the data I've got the following function in my service:
getApplications(): Observable
{
let apiURL: string = this.baseUrl + 'usermanagement/applications/read';return this.http.get(apiURL);
}
And finally my component calls that service function:
getApps(): void
{
this.appsService.getApplications()
.subscribe(data =>
{
this.apps = data.sort((a, b) => a.DisplayOrder - b.DisplayOrder);
});}
So the problem was that
data
structure matches the JSON capitalization and not my local TS object one, and I had to useDisplayOrder
notdisplayOrder
(and TS now whinges at me). But isn't Angular now supposed to map and convert JSON automatically (I know this was a specific step in the past)? And surely your local objects don't have to match the case sensitivity of the JSON provided by others.