访问Angular的API

发布于 2025-02-10 09:53:17 字数 966 浏览 3 评论 0原文

我正在尝试第一次使用API​​。这是我当前称其为:

url = 'https://data.economie.gouv.fr/api/records/1.0/search/?dataset=prix-carburants-fichier-instantane-test-ods-copie&q=aire+sur+l%27adour&lang=fr&facet=id&facet=adresse&facet=ville&facet=prix_maj&facet=prix_nom&facet=com_arm_name&facet=epci_name&facet=dep_name&facet=reg_name&facet=services_service&facet=horaires_automate_24_24&refine.ville=Aire-sur-l%27Adour';
datas = [];

constructor(private http: HttpClient){
  this.http.get(this.url).toPromise().then((data: any) => {
    this.datas = data
  })
}

和html:

<pre class="text-white">
{{ datas | json }}
</pre>

结果显示这样的json:

“

现在,如何我可以访问吗?我已经尝试了类似的事情:

let data of datas :

data.records
data[0][records]
etc

I'm trying to use an API for the first time. Here is how I currently call it :

url = 'https://data.economie.gouv.fr/api/records/1.0/search/?dataset=prix-carburants-fichier-instantane-test-ods-copie&q=aire+sur+l%27adour&lang=fr&facet=id&facet=adresse&facet=ville&facet=prix_maj&facet=prix_nom&facet=com_arm_name&facet=epci_name&facet=dep_name&facet=reg_name&facet=services_service&facet=horaires_automate_24_24&refine.ville=Aire-sur-l%27Adour';
datas = [];

constructor(private http: HttpClient){
  this.http.get(this.url).toPromise().then((data: any) => {
    this.datas = data
  })
}

And HTML :

<pre class="text-white">
{{ datas | json }}
</pre>

The result shows a JSON like this :

result

Now, how do I access it? I already tried things like :

let data of datas :

data.records
data[0][records]
etc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2025-02-17 09:53:17

这里是一个示例,其中JSON属性纠缠了您的整个对象:

 <span *ngFor="let element of json.records">
    {{element.datasetid }}
    {{element.geometry }}
    {{element.recordid }}
 </span>

Here an example, where json property rappresents your entire object:

 <span *ngFor="let element of json.records">
    {{element.datasetid }}
    {{element.geometry }}
    {{element.recordid }}
 </span>
红颜悴 2025-02-17 09:53:17

在这里,请尝试创建一种新方法并使用异步/等待。

创建一个类似的新方法 -

public async getData()
  {
     await this.http.get(this.URL)
    .toPromise()
    .then(response => this.datas = response['records'])
    .catch(err => { console.log ('error');
    });
  }

现在,您可以从构造函数上调用此方法类似的内容 -

constructor(private http: HttpClient) {
 this.getData()
  }

现在,您应该使用 *ngfor指令在数据群上迭代,因为它是一个数组,因此您可以使用此数据来开发HTML。

<div *ngFor="let data of datas">
  {{data?.fields?.id}}
</div>

这样,您可以使用此。
让我知道您是否需要进一步的帮助。

请找到stackblitz-- https:// stackblitz.com/edit/angular-dukmlc?file=src/app/app.component.ts

谢谢。

Here, Please try to create a new method and use async/await.

Create a new method something like this -

public async getData()
  {
     await this.http.get(this.URL)
    .toPromise()
    .then(response => this.datas = response['records'])
    .catch(err => { console.log ('error');
    });
  }

Now, You can call this method from your constructor something like this -

constructor(private http: HttpClient) {
 this.getData()
  }

Now, You should use *ngFor directive to iterate over the datas as it is an array so you can use this data to develop HTML.

<div *ngFor="let data of datas">
  {{data?.fields?.id}}
</div>

In this way, you can use this .
let me know if you need any further help on this .

please find the working link of stackblitz- https://stackblitz.com/edit/angular-dukmlc?file=src/app/app.component.ts

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文