Utilizing an API, I receive a JSON string that I aim to deserialize within my Angular 6 website.
Due to my unfamiliarity with how dictionaries function in TypeScript, I tend to steer clear of them when dealing with API responses.
Upon executing the following code:
console.log(JSON.parse(viewed_availab));
The output appears as follows:
[{
"2019-05-10T00:00:00": {
"TotalStopsCount": null,
"TotalRoutesCount": null,
"TotalStopsCountAfterBooking": null,
"OccupancyRate": null,
"OccupancyRateAfterBokking": null,
"ZoneId": null,
"DomainName": "MTH",
"Date": "2019-05-10T00:00:00",
"IsAvailable": false,
"Status": {
"Message": "Not enough time for items to reach final destination",
"StatusID": 1
},
"IdDomains": null,
"Items": []
}
}]
This is the request I make to obtain the aforementioned string:
this.dataService.post('cleard.api.timeslot.viewed_availabilities', new ViewedAvailabilityParams(search.postalCode, search.userName, search.date, search.businessUnitId, search.originLocationId))
.then((viewed_availab: string) => {
...
}).catch((error) => { this.searching = false; });
To better structure the response, I am aiming for a TypeScript dictionary format:
Dictionary<Date, Availibility> // shown in TypeScript syntax.
In this setup, the date serves as the key (taken from the first line of the Json), while availability represents the corresponding data:
{"TotalStopsCount": null,
"TotalRoutesCount": null,
"TotalStopsCountAfterBooking": null,
"OccupancyRate": null,
... and so forth }
(Note: A model has already been created for Availability).