0

I am having difficulties retrieving data from .json file in this format, I have tried headers, changing URL but it is not working:

{
    "cars": ["BMW", "Mercedes", "Fiat"],
    "testDrives": [
        {
            "start": "2020-02-03T11:00:00.000Z",
            "end": "2020-02-03T16:00:00.000Z",
            "name": "Bmw"
        },
        {
            "start": "2020-02-03T09:00:00.000Z",
            "end": "2020-02-03T12:30:00.000Z",
            "name": "Mercedes"
        }
  ]
}

My code:

 data = '../../assets/test.json';

  constructor(private http: HttpClient) { }

  getData() {
    this.http.get(this.data).subscribe(data => {
      console.log(data);
    });
  }

I keep getting this error: error

1 Answer 1

1

You need to define an interface with required properties to specify the response type

export interface ResponseModel{
  cars: string;
  testDrives: string;
}

and specify Response type in subscribe

getData() {
  this.http.get(this.data).subscribe((data: ResponseModel) => {
   console.log(data);
  });
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.