Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
角度6-解析JSON_Json_Angular_Asp.net Core Webapi - Fatal编程技术网

角度6-解析JSON

角度6-解析JSON,json,angular,asp.net-core-webapi,Json,Angular,Asp.net Core Webapi,我在使用.net core SPA应用程序时遇到问题。 -API调用正在传回结果 -SPA没有处理结果 以下是相关代码: SPA ts: class TestLibraryItem { private _apiPath: string; private _http: HttpClient; public name: string; public testResults: TestResult; constructor(name: string, apiPath: stri

我在使用.net core SPA应用程序时遇到问题。 -API调用正在传回结果 -SPA没有处理结果

以下是相关代码:

SPA ts:

class TestLibraryItem {
  private _apiPath: string;
  private _http: HttpClient;

  public name: string;
  public testResults: TestResult;

  constructor(name: string, apiPath: string, http: HttpClient) {
    this.name = name;
    this._apiPath = apiPath;
    this._http = http;
  }

  RunTests() {
    this._http.get<TestResult>(this._apiPath)
      .subscribe(result => {
      this.testResults = result;
      console.log(this.testResults);
      console.log(this.testResults.CheckName);
    });
  }
}

class TestResult {
  CheckName: string;
  Checks: CheckResult[];
}

class CheckResult {
  Test: string;
  Pass: boolean;
}

据我所知,我正在从API获取有效的json(由console.log吐出来表示,但实际上它并没有构建导致未定义的对象。

我认为JSON中的属性是从大写到小写解析的-
CheckName->CheckName
。因为Javascript/Typescript是一种区分大小写的语言,所以需要使用不同的属性名称

尝试使用小写字母登录,并将属性名称更改为以小写字母开头。在
Javascript/Typescript
中,通过小写字母启动函数和变量/属性名称是常见的标准

console.log(this.testResults.checkName);

由于首先触发了此console.log(this.testResults),因此您将无法定义

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']);
    });
  }
RunTests(){
this.\u http.get(this.\u apiPath)
.订阅(结果=>{
this.testResults=结果;
log(this.testResults);
console.log(this.testResults.CheckName==未定义?“”:this.testResults['CheckName']);
});
}
或者使用SetTimeOut

 RunTests() {
        this._http.get<TestResult>(this._apiPath)
          .subscribe(result => {
          this.testResults = result;
          console.log(this.testResults);
         setTimeout(()=>{console.log(this.testResults['CheckName'])},2000); 

        });
      }
RunTests(){
this.\u http.get(this.\u apiPath)
.订阅(结果=>{
this.testResults=结果;
log(this.testResults);
setTimeout(()=>{console.log(this.testResults['CheckName'])2000);
});
}

我有一个类似的问题,即它看起来像一个有效的json响应,但实际上是一个“文本”响应。请尝试以下内容:

  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'
    });
  }
  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'
    });
  }