首页 > 解决方案 > Parse complex JSON in Typescript

问题描述

The response of a request delivers a JSON. The structure of the JSON looks like this:

{
"32": {
    "docKey": "32",
    "outletId": 32,
    "mdngOutlet": {
        "outletBasic": {
            "outletId": 32,
        }
    }
},
"33": {
    "docKey": "32",
    "outletId": 32,
    "mdngOutlet": {
        "outletBasic": {
            "outletId": 32,
        }
    }
},
"34": {
    "docKey": "32",
    "outletId": 32,
    "mdngOutlet": {
        "outletBasic": {
            "outletId": 32,
        }
    }
},
"35": {
    "docKey": "32",
    "outletId": 32,
    "mdngOutlet": {
        "outletBasic": {
            "outletId": 32,
        }
    }
},
}

What does the interface look like? 32, 33, 34, ... seem to act like map. How can you use a map in an interface?

标签: jsontypescriptinterface

解决方案


Typescript 界面可以有动态键。

interface YourJSON{
    [key: string]: yourObjectInterface
}

推荐阅读