Exploring the Angular2 tutorial "Tour of Heroes" showcases how to handle JSON data within a promise. But what if the JSON structure is more complex?
JSON:
let complexMessage = [{
"heroes":[
{id: 11, name: 'Mr. Nice'},
{id: 12, name: 'Narco'},
{id: 13, name: 'Bombasto'},
{id: 14, name: 'Celeritas'},
{id: 15, name: 'Magneta'},
{id: 16, name: 'RubberMan'},
{id: 17, name: 'Dynama'},
{id: 18, name: 'Dr IQ'},
{id: 19, name: 'Magma'},
{id: 20, name: 'Tornado'}
]
,"numHeroes": 9
,"messages":[
{message: "aaa", args:[]},
{message: "bbb", args:[]}
]
}];
The provided Typescript snippet encounters issues with handling multidimensional JSON:
getHeroes(): Promise<Hero[]> {
return this.http.get(this.heroesUrl)
.toPromise()
.then(response => response.json())
.catch(this.handleError);
Then we see:
getHeroes() {
this.heroService
.getHeroes()
.then(heroes => this.heroes = heroes)
.catch(error => this.error = error);
}
Is there a way to extract and assign the inner "heroes" array to this.heroes in the code above? (The original tutorial can be found at https://angular.io/docs/ts/latest/tutorial/toh-pt6.html)