在为以下源JSON格式创建打字稿类时,我需要一些帮助。
Control数组中可能有多个组,字段或操作。在这一点上,我正在努力创建以下格式的打字稿类表示形式。我认为类是组,字段和动作,并使控件数组为任何类型,然后基于对象名称(例如组,字段或动作)创建相应类的实例。是这样做的正确方法,还是任何人都有更好的方法?
{
"controls": [
{
"group": {
"name": "Building",
"maximum": "*",
"minimum": "0",
"rows": [
{
"id": "b8D7D15B8029E4909B322719EBAC448B8",
"controls": [
{
"field": {
"name": "BuildingDescription",
"value": "Buidling#1-123 Street",
"reference": "b8D7D15B8029E4909B322719EBAC448B8",
"product": "Product1",
"caption": "BuildingDescription",
"dataType": "string",
"controlType": "text",
"format": "",
"maxLength": "0",
"readOnly": "1",
"hidden": "0",
"required": "0"
}
}
]
}
]
}
},
{
"field": {
"name": "AccountName",
"value": "",
"reference": "a22AF4E5BE8BA4649A528E4FF7EAEC2C2",
"product": "Product1",
"caption": "AccountName",
"dataType": "string",
"controlType": "text",
"format": "",
"maxLength": "50",
"readOnly": "0",
"hidden": "0",
"required": "0"
}
},
{
"action": {
"id": "Account_3D",
"description": "",
"caption": "ViewAccount",
"contentType": "application/json",
"hidden": "0",
"methods": [
{
"index": 0,
"type": "PUT",
"pageSet": "PagesetAccount",
"page": "Account",
"uri": "http: //server/pageSets/PagesetAccount/pages/Account.json"
},
{
"index": 1,
"type": "GET",
"pageSet": "PagesetAccount",
"page": "Account",
"uri": "http: //server/pageSets/PagesetAccount/pages/Account.json?command=view"
}
]
}
}
]
}
任何帮助将不胜感激!
答案 0 :(得分:0)
您可以使用
// strict
type A = {
controls: [
{
group: {
name: string,
maximum: string,
minimum: string,
rows: [
{
id: string,
controls: [
{
field: {
name: string,
value: string,
reference: string,
product: string,
caption: string,
dataType: string,
controlType: string,
format: string,
maxLength: string,
readOnly: string,
hidden: string,
required: string,
}
}
]
}
]
}
},
{
field: {
name: string,
value: string,
reference: string,
product: string,
caption: string,
dataType: string,
controlType: string,
format: string,
maxLength: string,
readOnly: string,
hidden: string,
required: string,
}
},
{
action: {
id: string,
description: string,
caption: string,
contentType: string,
hidden: string,
methods: Array<{
index: number,
type: string,
pageSet: string,
page: string,
uri: string,
}>
}
}
]
}
或
// quite strict
type B = {
controls: Array<Record<string, Record<string, string | number | Array<Record<string, string | number | Array<Record<string, Record<string, string>>>>>>>>
};
或
// Readable
type C = { controls: Array<object> };
您可以使用接口代替类型或类