read data from json file in angular using http code example
Example 1: angular load json file with httpclient
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class AppSettingsService {
constructor(private http: HttpClient) {
this.getJSON().subscribe(data => {
console.log(data);
});
}
public getJSON(): Observable<any> {
return this.http.get("./assets/mydata.json");
}
}
Example 2: get data from json file angular
import variableName from '../../assets/fileName.json';
export class AppComponent {
public nameList:{field1:type, field2:type}[] = variableName;
}
declare module "*.json" {
const value: any;
export default value;
}