访问Angular的API
我正在尝试第一次使用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 :
Now, how do I access it? I already tried things like :
let data of datas :
data.records
data[0][records]
etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里是一个示例,其中JSON属性纠缠了您的整个对象:
Here an example, where json property rappresents your entire object:
在这里,请尝试创建一种新方法并使用异步/等待。
创建一个类似的新方法 -
现在,您可以从构造函数上调用此方法类似的内容 -
现在,您应该使用 *ngfor指令在数据群上迭代,因为它是一个数组,因此您可以使用此数据来开发HTML。
这样,您可以使用此。
让我知道您是否需要进一步的帮助。
请找到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 -
Now, You can call this method from your constructor something like this -
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.
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.