我有一个从 api 返回的 json 对象,我想用该对象包含的字段创建一个接口(interface)。我正在使用 ionic 3 框架。我需要有关如何创建此接口(interface)的帮助。(我很困惑:我应该为数据创建另一个接口(interface)吗?如果是,如何将它包含在主接口(interface)中?)对象结构如下:
{
"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),则应为响应中的每个对象定义一个接口(interface)。您不必这样做,但要获得正确的类型完成,您应该这样做。
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 - 对象 typescript/ionic 3 的接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50866790/