如何使用Angular模型来解析具有通用名称的JSON对象



我正在将模型创建到一个角度项目中,以解析一些服务的JSON数据。

JSON文件返回这样的信息:

[
    {
        "bronze": [
            {
                "name": "Pontuau00e7u00e3o",
                "now": "7.000",
                "enabling": "1.800",
                "icon": "score",
                "status": 1
            },
            {
                "name": "Cancelamento",
                "now": "2.5%",
                "enabling": "5.0%",
                "icon": "cancellation",
                "status": 1
            }
        ]
    },
    {
        "silver": [
            {
                "name": "Pontuau00e7u00e3o",
                "now": "7.000",
                "enabling": "4.000",
                "icon": "score",
                "status": 1
            },
            {
                "name": "Cancelamento",
                "now": "2.5%",
                "enabling": "5.0%",
                "icon": "cancellation",
                "status": 1
            },
            {
                "name": "Assinatura Digital",
                "now": "79.5%",
                "enabling": "5.0%",
                "icon": "cancellation",
                "status": 1
            }
        ]
    }
]

我想告诉Angular,对象的名称无关紧要,这样:

export interface Checklist {
   anyObject: ChecklistItems[];
}
export interface ChecklistItems {
    name:     string;
    now:      string;
    enabling: string;
    icon:     string;
    status:   number;
}

这是可能的吗?

实际上是一个打字稿问题,答案正在使用索引签名:

interface Checklist {
  [key: string]: ChecklistItems[];
} 

im以这种方式 - 您可以将任何键作为字符串,带有指定的值。

最新更新