javascript - 对象 typescript/ionic 3 的接口
问题描述
我有一个从 api 返回的 json 对象,并希望创建一个包含该对象包含的字段的接口。我正在使用 ionic 3 框架。我需要有关如何创建此接口的帮助。(我很困惑:我应该为数据创建另一个接口吗?如果是,如何将其包含在主界面中?)对象结构如下:
{
"status": "success",
"data": [
{
"id": 113,
"subject": "hello there",
"body": "i am hisham",
"sender": {
"id": 51,
"country": {
"id": 9,
"name_en": "Syria",
}
}
},
{
"id": 114,
"subject": "hello there",
"body": "i am lkfdj",
"sender": {
"id": 54,
"country": {
"id": 9,
"name_en": "Syria",
}
}
}
]
}
解决方案
如果要定义接口,则应为响应中的每个对象定义一个。您不必这样做,但要获得正确的类型完成,您应该这样做。
interface Response {
status: string;
data: Data[];
}
interface Data {
id: number;
subject: string;
body: string;
sender: Sender;
}
interface Sender {
id: number;
country: Country;
}
interface Country {
id: number;
name_en: string;
}
推荐阅读
- javascript - 将文件作为 JSON 上传到 Python 网络服务器
- javascript - 用 mySql 结果刷新 div
- c - 如何在 sscanf 中使用 set scanset 读取 0-9 等数字
- java - 迭代缓存时,Ignite 消耗所有内存并因 OutOfMemory 失败
- c# - 如何对所有文件进行 VScode .net 核心调试,而不仅仅是选定的文件
- python - pandas.DataFrame 列中值组合的可能性
- android - 两天之间难以从 Sqlite 数据库中获取数据
- google-sheets - 如何将多行应用于下拉菜单-Google-Spreadsheet 的条件选择?
- python - 如何使用 Python 连接到 Oracle hyperion (HFM)?
- javascript - 单引号 (') 后的原子红色突出显示