0

I have a JSON below :

{
  "sports": [
    { "id": 1, "name": "Soccer" },
    { "id": 2, "name": "Basketball" }
  ]
}

How I can read the id and the name in typeScript ? Currently I use

let result = this.dataService.getJSON()

public getJSON(): Observable<any> {
  return this.http.get('./assets/json/data.json');
}

But I have a error message

map is not a function

I don't know How I can parse the JSON,

I would like have this result.

[
  { "id": 1, "name": "Soccer" },
  { "id": 2, "name": "Basketball" }
]

Thank for your help.

Heptagram
  • 43
  • 7

1 Answers1

0

If you are using Observables, you need to subscribe for thread to receive a response:

this.dataService.getJSON().subscribe(data => {
  console.log(data);
});
demkovych
  • 4,124
  • 2
  • 13
  • 24