如何从我们从Angular 6中的API获取的响应中获取字段

时间:2018-09-07 07:53:01

标签: angular

我的问题是我在Web API上没有任何getAll()方法。当我点击Angular应用程序时,唯一能看到的就是在网络上,整个JSON都可以在网络http:localhost:8080/api上使用。现在,我只想要一个字段,即获取的json中的contractId

{
  "contractId": 44,
  "contractName": "BOND",
  "contractServiceList": [
    {
      "id": 44,
      "serviceId": 1,
      "providerTier": null,
      "coinsurance": 35,
      "copay": 547,
      "penaltyApplies": "Y",
      "penaltyRule": "Non Emergency ER Use",
      "penaltyType": "Coinsurance",
      "penaltyValue": 890,
      "deductibleApplies": "Y"
    }
  ]
}

有人可以帮我实现这一目标吗?

所有其他字段都将从UI提交为“表单提交”,但Id是在后端自动生成的,其值是我想要的表单提交时的值。

1 个答案:

答案 0 :(得分:1)

订阅以获取contractId

我假设您的请求方法为 GET ,而响应json 为:

{
  "contractId": 44,
  "contractName": "BOND",
  "contractServiceList": [
    {
      "id": 44,
      "serviceId": 1,
      "providerTier": null,
      "coinsurance": 35,
      "copay": 547,
      "penaltyApplies": "Y",
      "penaltyRule": "Non Emergency ER Use",
      "penaltyType": "Coinsurance",
      "penaltyValue": 890,
      "deductibleApplies": "Y"
    }
  ]
}

获取数据:

服务中:

import {
    HttpClient,
} from '@angular/common/http';

constructor(private http: HttpClient) {}

this.http.get('http:localhost:8080/api').subscribe((resp: any) => {
    console.log(resp.contractId)
});

参考===> https://angular.io/guide/http