arrays - 如何在 api 中将 JSON 对象转换为 Typescript 数组
问题描述
我有一个返回以下内容的 API 请求:
{
"FarmerDetails": [
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
},
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
}
]
}
我需要在单击按钮时显示标题,并且当按钮单击此“fetchPeople()”函数时会被调用。
fetchPeople() {
this.peopleService.fetchPeople().subscribe((res:any)=>{
this.people$ = res;
});
}
如何在打字稿中将我的对象转换为数组并显示特定或所有值。
解决方案
当您从 api 获得响应时,它将以字符串格式返回。因此,您需要将其转换为 json 格式。将其解析为 json:
this.people=JSON.parse(res);
推荐阅读
- python - 当我有一个数据框时,为什么 Python 会告诉我 **TypeError: unhashable type: 'list'**?
- python - Webdriver不返回一些数据
- c# - CS0120 大型文件列表 C# Discord webhook
- wpf - WinForms/WPF 控件的全局唯一标识符?
- web3 - 使用 Web3 读取 LP 池的余额
- anaconda - Pytorch 在 conda 上安装失败,错误为 -CondaHTTPError: HTTP 403 FORBIDDEN for url
- excel - 使用自动过滤器删除行时面临问题
- ruby - 有没有更有效的方法来使用 Ruby 控制控制台光标(马车位置等)?
- elasticsearch - 如何在elasticsearch中索引csv文档?
- ios - SwiftUI - 如何从不同的视图中获取 GeometryReader 的大小/高度?