Cin*_*ode 2 json typescript angular
我在Angular应用程序中接收JSON对象:我想获取一些值,但问题是我无法创建TypeScript接口来匹配这些值,因为对于一个键,值可以是包含多个值的数组,也可以只是一个简单的值(见"关系").
一个例子:
{
"ID": "123",
"name": "dummy",
"relations": [
{ "rel": "child", "relValue": ["child1", "child2"] },
{ "rel": "parent", "relValue": "parent"}
]
}
Run Code Online (Sandbox Code Playgroud)
无论是否有数组或单个值,如何检索此键的所有元素("关系")的值?
这是您可以使用的类型
type Type = {
ID: string,
name: string,
relations: Array<Record<string, string | string[]>>,
}
Run Code Online (Sandbox Code Playgroud)
或更精确
type Type = {
ID: string,
name: string,
relations: Array<{ rel: string, relValue: string | string[] }>,
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47 次 |
| 最近记录: |