2016-08-01 29 views
0

我在期待下面的示例來自API的JSON響應,並且我試圖爲它創建一個接口。Angular 2試圖爲預期的API響應創建接口

{ 
    types: [ 
    { 
     id: 1, 
     contents: [ 
     { 
      id: 1001, 
      perishable: 0 
     }, 
     { 
      id: 1002, 
      perishable: 0 
     } 
     ] 
    } 
    ] 
} 

這是我如何試圖定義接口,但我不知道我是否正在做這個權利。基本上,我得到的迴應是「類型」,它是一個對象數組,其中的每個對象都有一個名爲「contents」的數組。另外,如何說什麼是一組對象?

export interface Types { 
    id: string; 
    contents: Array<any>; 
} 

是嗎?

回答

2

您可以使用any如果你想,但你可以更具體:

interface Content { 
    id: number; 
    perishable: number; 
} 

interface Type { 
    id: number; 
    contents: Content[]; 
} 

interface Response { 
    types: Type[]; 
}