捕获 http json body 响应并在 angular ionic5 中转换为数组
Capture http json body response and convert into array in angular ionic5
我被卡住了,在浏览这里和其他页面时我找不到合适的东西:/
我想要的是提取它并以一种我可以用来在 html 中构建一个简单的 table 看起来像
的方式使用它
response
count
[value]
[value]
[value]
[value]
并以列表格式保存,以便稍后在 chart.js 中使用,但那将是稍后的事情。
(仅供参考,我不是开发人员,一路学习 - 但我擅长乐高 :) 所以连接积木和适应是我学习编码的方式,如果初学者有问题,我深表歉意 :))
所以,我有一个 HTTP 调用 (GET) 工作。它 returns 正文:
[
{
"response": "Yes",
"count": 1
},
{
"response": "No",
"count": 1
}
]
但我不知道如何使用它...
我的http服务查询:
getPollResults(pollId): Observable<Pollresults>{
return this.http
.get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
.pipe(
retry(2),
catchError(this.handleError)
)
}
我的投票结果模型:
export class Pollresults {
response: string;
count: number;
}
我的组件 page.ts 调用似乎捕获了数据,但后来我陷入了如何使用它的困境...
export class PollResultsPage implements OnInit {
uid: string;
dataPollresults: Pollresults;
constructor(
private httpConfigService: HttpConfigService
) { }
ngOnInit() {
// this.id = this.activatedRoute.snapshot.params["id"];
this.uid = "alphapoll";
//get item details using id
this.httpConfigService.getPollResults(this.uid).subscribe(response => {
console.log(response);
this.dataPollresults = response;
//data => resolve(Object.keys(data).map(key => data[key]))
})
}
关于如何正确提取和获取数组的任何帮助?以及如何将其添加到 html?
非常感谢您的帮助。
改变这个
getPollResults(pollId): Observable<Pollresults>{
return this.http
.get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
.pipe(
retry(2),
catchError(this.handleError)
)
}
对此
getPollResults(pollId): Observable<Pollresults[]>{
return this.http
.get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
.pipe(
retry(2),
catchError(this.handleError)
)
}
我被卡住了,在浏览这里和其他页面时我找不到合适的东西:/ 我想要的是提取它并以一种我可以用来在 html 中构建一个简单的 table 看起来像
的方式使用它response | count |
---|---|
[value] | [value] |
[value] | [value] |
并以列表格式保存,以便稍后在 chart.js 中使用,但那将是稍后的事情。
(仅供参考,我不是开发人员,一路学习 - 但我擅长乐高 :) 所以连接积木和适应是我学习编码的方式,如果初学者有问题,我深表歉意 :))
所以,我有一个 HTTP 调用 (GET) 工作。它 returns 正文:
[
{
"response": "Yes",
"count": 1
},
{
"response": "No",
"count": 1
}
]
但我不知道如何使用它...
我的http服务查询:
getPollResults(pollId): Observable<Pollresults>{
return this.http
.get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
.pipe(
retry(2),
catchError(this.handleError)
)
}
我的投票结果模型:
export class Pollresults {
response: string;
count: number;
}
我的组件 page.ts 调用似乎捕获了数据,但后来我陷入了如何使用它的困境...
export class PollResultsPage implements OnInit {
uid: string;
dataPollresults: Pollresults;
constructor(
private httpConfigService: HttpConfigService
) { }
ngOnInit() {
// this.id = this.activatedRoute.snapshot.params["id"];
this.uid = "alphapoll";
//get item details using id
this.httpConfigService.getPollResults(this.uid).subscribe(response => {
console.log(response);
this.dataPollresults = response;
//data => resolve(Object.keys(data).map(key => data[key]))
})
}
关于如何正确提取和获取数组的任何帮助?以及如何将其添加到 html?
非常感谢您的帮助。
改变这个
getPollResults(pollId): Observable<Pollresults>{
return this.http
.get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
.pipe(
retry(2),
catchError(this.handleError)
)
}
对此
getPollResults(pollId): Observable<Pollresults[]>{
return this.http
.get<Pollresults>(this.base_path + '/polls/responses/' + pollId)
.pipe(
retry(2),
catchError(this.handleError)
)
}