After generating a string with the correct structure that includes an array, I am able to navigate through the JSON on sites like
However, when attempting to access the array, it turns out that the array itself is null.
Here is the scenario:
Firstly, the interfaces are defined as follows:
export interface IPlayerJSON {
idPlayer: number,
name: string;
email: string,
arrayAvatar: Array<IAvatarJSON>,
};
export interface IAvatarJSON {
idAvatar: number,
nickName: string,
original: boolean,
signature: string,
};
Followed by the overridden method:
public toJSON(): IPlayerJSON {
let json: IPlayerJSON;
json = {idPlayer: this.idPlayer, email: this.email, name: this.name, avatarOriginal: this.avatarOriginal.toJSON(),
arrayAvatar: []}
for (let r of this.arrayAvatar) {
json.arrayAvatar.push(r.toJSON());
};
return json;
};
The Avatar object also has a modified toJSON method. The JSON.stringify function generates a correct and navigable JSON output.
However, when trying to rehydrate:
public static playerRehydrate(json: IPlayerJSON): Player {
let player : Player = new Player(null, null, null);
console.log('JSON ', json); // Perfect, with an array
player.setEmail(json.email);
player.setPlayerID(json.idPlayer);
player.setName(json.name);
console.log('Array Avatar ', json.arrayAvatar);
for (let r of json.arrayAvatar) { // Throws error, length of undefined
player.addAvatar(RehydrateService.avatarRehydrate(r));
};
return player;
};
The issue arises because json.arrayAvatar is null and interaction with it becomes impossible.
Any suggestions or tips would be greatly appreciated. Thank you!