Angular 6 - Parsing JSON
I think your properties in the JSON are parsed from upper case to lower case - CheckName -> checkName
. As Javascript/Typescript is a case sensitive language you need to different property names.
Try to log with lower case and also change your property names to start with lower case. It is a common standard in Javascript/Typescript
to start function and variable/property names via lower case.
console.log(this.testResults.checkName);
You are getting undefined because this console.log(this.testResults) is fired first
RunTests() { this._http.get<TestResult>(this._apiPath) .subscribe(result => { this.testResults = result; console.log(this.testResults); console.log(this.testResults.CheckName === undefined ? '' : this.testResults['CheckName']); }); }
or use SetTimeOut
RunTests() { this._http.get<TestResult>(this._apiPath) .subscribe(result => { this.testResults = result; console.log(this.testResults); setTimeout(()=>{console.log(this.testResults['CheckName'])},2000); }); }
I had a similar issue i.e. it looked lika valid json response but in fact it was a "text" response. Give the following a try:
getdData(inParams) { let headers = new HttpHeaders(); headers = headers.append('Content-Type', 'application/json'); // need responseType = text (non object) return this.http.get(environment.url, { headers, responseType: 'text' }); }