简体   繁体   English

JSON对象到Typescript类

[英]JSON Object to Typescript Class

I need some help in creating typescript classes for below source JSON format. 在为以下源JSON格式创建打字稿类时,我需要一些帮助。

There could be multiple groups, fields or action within Control array. 控制数组中可能有多个组,字段或操作。 At this point i am struggling to create typescript class representation out of below format.I am think of classes as group, field and action and make controls array to be of type any and then based on object name like group, field or action i have to create instance of corresponding class. 在这一点上,我正在努力创建以下格式的打字稿类表示形式。我认为类是组,字段和动作,并使控件数组为任何类型,然后基于对象名称(例如组,字段或动作)创建相应类的实例。 Is this right way of doing it or do anyone has better approach. 是这样做的正确方法,还是任何人都有更好的方法。

{
    "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"
                    }
                ]
            }
        }
    ]
}

Any help would be much appreciated! 任何帮助将非常感激!

You can use 您可以使用

// 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,
                }>
            }
        }
    ]
}

or 要么

// quite strict
type B = {
    controls: Array<Record<string, Record<string, string | number | Array<Record<string, string | number | Array<Record<string, Record<string, string>>>>>>>>
};

or 要么

// Readable
type C = { controls: Array<object> };

You can use interface instead of type, or class as well 您可以使用接口代替类型,也可以使用类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM