[英]Display API JSON response in template - Angular
I want to display data on my website from JSON file from URL.我想在我的网站上显示来自 URL 的 JSON 文件的数据。 I work on Angular and I've create a HttpClient in my component.我在 Angular 上工作,并在我的组件中创建了一个 HttpClient。 Code below show all document in console, so here is my question.下面的代码在控制台中显示所有文档,所以这是我的问题。
let resp = this.http.get("https://spreadsheets.google.com/feeds/cells/1w2vpgbwImaQGCbpMqZ3P0NC93tuVved0oOFc9Zr22dU/1/public/full?alt=json");
resp.subscribe((data)=>console.log(data));
Can I show a specify elements from this JSON file?我可以显示此 JSON 文件中的指定元素吗?
I want to display data from: feed -> entry -> gs$cell -> $t on my website.我想在我的网站上显示来自:feed -> entry -> gs$cell -> $t 的数据。 How I should start, what I need?我应该如何开始,我需要什么?
I have add a picture how the JSON array looks and what elements I want to get and show on my website.我添加了一张图片 JSON 数组的外观以及我想要在我的网站上获取和显示的元素。
Store API response in a class variable ie data
, assume sample data将 API 响应存储在 class 变量中,即data
,假设样本数据
data = {
feed: {
entry: [
{ gs$cell: { $t: 'Nazwa' } },
{ gs$cell: { $t: 'pazwa' } },
{ gs$cell: { $t: 'shuzwa' } }
]
}
}
Accessing directly in template直接在模板中访问
<div *ngFor="let d of this.data.feed.entry">{{d.gs$cell.$t}}</div>
It seems to me that your problem is how to extract the data.在我看来,您的问题是如何提取数据。
I can't use your HttpClient so I will be using fetch
API for demonstration.我无法使用您的 HttpClient,因此我将使用fetch
API 进行演示。
async function getData() { const endpoint = "https://spreadsheets.google.com/feeds/cells/1w2vpgbwImaQGCbpMqZ3P0NC93tuVved0oOFc9Zr22dU/1/public/full?alt=json"; // const json = await http.get(endpoint); const response = await fetch(endpoint); const json = await response.json(); const { feed } = json; const { entry: feedEntries } = feed; return feedEntries.map((entry) => { const { gs$cell } = entry; const { $t } = gs$cell; return $t; }); } (async () => { const data = await getData(); console.log(data); // do whatever you wan't with the data, use it on your template maybe? })();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.