Sanyam Madaan
Sanyam Madaan

Reputation: 139

How to convert JSON Array of Objects to Observable Array in angular

I am calling a rest API from angular, the rest api returns data in JSON Array of Objects like this enter image description here

Now I am trying to convert it to my model class array but don't getting anywhere can you please guide me.

My Model Class

enter image description here

My Service File

enter image description here

Here it gives error on map and I don't know how to convert it to my model class array to display it in table

Upvotes: 0

Views: 1902

Answers (3)

Sanyam Madaan
Sanyam Madaan

Reputation: 139

Resolved It.

As i was directly getting the array of objects in response, I don't need to convert it and use an interface. So here is my correct code

fetchAllGamesRecord() : Observable<Fifa[]>{
    const fifaUrl = `${this.baseUrl}/fetchAllGamesRecord`;
    return this.httpClient.get<Fifa[]>(fifaUrl);
  }

This function is called like this

this.fifaService.fetchAllGamesRecord().subscribe(
      data => {
        this.allGameRecord = data;
        console.log(`Data = `+data);
      }
    );

Upvotes: 1

Pavan Kumar Jorrigala
Pavan Kumar Jorrigala

Reputation: 3085

In the Spring framework also Fifa[] doesn't work in REST calls, we need to put it in another class as I showed below, I assume this will work.

export class FifaWrapper {
    wrapper: Fifa[];
}

and also use this class in Observable

fetchAllGamesRecord():  Observable<FifaWrapper> {
 ... do you handling here
}

Upvotes: 0

Shlok Nangia
Shlok Nangia

Reputation: 2377

you can do that while calling fetchAllGamesRecord

fetchAllGamesRecord().subscribe( (response: Fifa[]) => {
  // do something
}

where Fifa is interface not class

Upvotes: 0

Related Questions