-5

in angular Our application access JSON file which is stored on server side. That JSON file contains information of students. After button press we have to fetch data from JSON file and display on web page

Rakesh
  • 1

2 Answers2

0

You could use the Http service to make an API call to your local env, and pointing to your JSON file, you could read it with an observable. Using something like this code:

@Injectable()
export class AppServices{

    constructor(private http: Http) {
         var obj;
         this.getJSON().subscribe(data => obj=data, error => console.log(error));
    }

    public getJSON(): Observable<any> {
         return this.http.get("./file.json")
                         .map((res:any) => res.json())
                         .catch((error:any) => console.log(error));

     }
}

You could read more here:
Angular4: how do access local json?

Joseph Arriaza
  • 12,014
  • 21
  • 44
  • 63
-1

As you are using Angular application, you can make an HTTP request to your server and the server would return you the required JSON response.

Once you get hold of the JSON, you may then set that on a variable in your .ts file and depending whether it is an array or just an object, you may use it accordingly using *ngFor or by just using the object directly.

For more information, please refer to the Angular tutorial:-

https://angular.io/tutorial/toh-pt6

Ankit Sharma
  • 1,674
  • 8
  • 11