javascript - json reorganization problem, more complicated
phpcn_u1582
phpcn_u1582 2017-05-19 10:24:26
0
3
668

1. Question: Convert json1 to json2 form
2. Description: There may be infinite layers in properties, type refers to the type, the array type has items, and the array may be an object array or other types.
json1:

 body: {
                "name": "body",
                "in": "root",
                "required": false,
                "description": "",
                "type": "object",  //数据类型
                "format": "",
                "properties": {
                    "bcc": {
                        "name": "bcc",
                        "in": "body",
                        "required": false,
                        "description": "暗送人列表",
                        "type": "array",
                        "format": "",
                        "properties": null,
                        "items": {
                            "name": "bcc.*",
                            "in": "body",
                            "required": false,
                            "description": "每个暗送人",
                            "type": "object",
                            "format": "",
                            "properties": {
                                "address": {
                                    "name": "bcc.*.address",
                                    "in": "body",
                                    "required": false,
                                    "description": "单个暗送人地址",
                                    "type": "string",
                                    "format": "email",
                                    "properties": null,
                                    "items": null
                                },
                                "name": {
                                    "name": "bcc.*.name",
                                    "in": "body",
                                    "required": false,
                                    "description": "单个暗送人名称",
                                    "type": "string",
                                    "format": "",
                                    "properties": null,
                                    "items": null
                                }
                            },
                            "items": null
                        }
                    },
                    "cc": {
                        "name": "cc",
                        "in": "body",
                        "required": false,
                        "description": "抄送人列表",
                        "type": "array",
                        "format": "",
                        "properties": null,
                        "items": {
                            "name": "cc.*",
                            "in": "body",
                            "required": false,
                            "description": "每个抄送人",
                            "type": "object",
                            "format": "",
                            "properties": {
                                "address": {
                                    "name": "cc.*.address",
                                    "in": "body",
                                    "required": false,
                                    "description": "单个抄送人地址",
                                    "type": "string",
                                    "format": "email",
                                    "properties": null,
                                    "items": null
                                },
                                "name": {
                                    "name": "cc.*.name",
                                    "in": "body",
                                    "required": false,
                                    "description": "单个抄送人名称",
                                    "type": "string",
                                    "format": "",
                                    "properties": null,
                                    "items": null
                                }
                            },
                            "items": null
                        }
                    },
                    "content": {
                        "name": "content",
                        "in": "body",
                        "required": true,
                        "description": "邮件内容",
                        "type": "string",
                        "format": "",
                        "properties": null,
                        "items": null
                    },
                    "from": {
                        "name": "from",
                        "in": "body",
                        "required": true,
                        "description": "发件人",
                        "type": "object",
                        "format": "",
                        "properties": {
                            "address": {
                                "name": "from.address",
                                "in": "body",
                                "required": true,
                                "description": "发件人地址",
                                "type": "string",
                                "format": "email",
                                "properties": null,
                                "items": null
                            },
                            "name": {
                                "name": "from.name",
                                "in": "body",
                                "required": true,
                                "description": "发件人名称",
                                "type": "string",
                                "format": "",
                                "properties": null,
                                "items": null
                            }
                        },
                        "items": null
                    },
                    "mimeType": {
                        "name": "mimeType",
                        "in": "body",
                        "required": true,
                        "description": "mime type",
                        "type": "string",
                        "format": "alphanum",
                        "properties": null,
                        "items": null
                    },
                    "subject": {
                        "name": "subject",
                        "in": "body",
                        "required": true,
                        "description": "主题",
                        "type": "string",
                        "format": "",
                        "properties": null,
                        "items": null
                    },
                    "to": {
                        "name": "to",
                        "in": "body",
                        "required": true,
                        "description": "收件人列表",
                        "type": "array",
                        "format": "",
                        "properties": null,
                        "items": {
                            "name": "to.*",
                            "in": "body",
                            "required": true,
                            "description": "每个收件人",
                            "type": "object",
                            "format": "",
                            "properties": {
                                "address": {
                                    "name": "to.*.address",
                                    "in": "body",
                                    "required": true,
                                    "description": "单个收件人地址",
                                    "type": "string",
                                    "format": "email",
                                    "properties": null,
                                    "items": null
                                },
                                "name": {
                                    "name": "to.*.name",
                                    "in": "body",
                                    "required": true,
                                    "description": "单个收件人名称",
                                    "type": "string",
                                    "format": "",
                                    "properties": null,
                                    "items": null
                                }
                            },
                            "items": null
                        }
                    }
                },
                "items": null
            },

2.json 2

body: {
 'bcc': [{'address':'单个暗送人地址', 'name':'单个暗送人名称'}],
  'cc': [{'address':'单个抄送人地址', 'name':'单个抄送人名称'}],
 "content": '邮件内容',
  "from": {'address':'发件人地址', "name":'发件人名称'},
"mimeType": "mime type",
"subject": "主题",
"to":[{'address':'单个收件人地址', 'name':'单个收件人名称'}]
}
phpcn_u1582
phpcn_u1582

reply all(3)
phpcn_u1582

Due to time constraints, I didn’t write too much details in the comments

    // 假设 a 就是json1, b 就是json2
    var a = {
        body: {...}
    };
    var b = {
        body: {...}
    };

    function recursion( data, o, type ) {
        // 这里规定下,最终递归出来的数据类型
        b.body[o] = {
            'object': {},
            'array' : [],
            'string': ''
        }[ type ];
        
        // 这里获取下面的新数据,
        var newData = {
            'object': data.properties,
            'array' : data.items,
            'string': data.description
        }[ data.type ];
    
        // 判断新数据是否符合递归条件,并获得最终数据
        if ( newData && newData.type ) {
            recursion( newData, o, type )
        }else{
            if ( type == 'object' || type == 'array' ) {
                for (let i in newData){
                    newData[i] = newData[i].description
                }
            };

            if ( type == 'array' ) {
                b.body[o].push(newData);
            } else {
                b.body[o] = newData;
            };
        };
    }

    var data = a.body.properties;
    for (let o in data) {
        // 从这里开始递归,
        recursion( data[o], o, data[o].type )
    }
    console.log(b);

滿天的星座

Tree recursive traversal is fine.

左手右手慢动作

Thanks for the invitation. . . .

function deepReadTree(param) {
        var child_param, cur_result;
        if (param.properties) {
            child_param = param.properties;
            cur_result = {};
            for (var key in child_param) {
                cur_result[key] = deepReadTree(child_param[key]);
            }
        } else if (param.items) {
            child_param = param.items.properties;
            cur_result = {};
            for (var key in child_param) {
                cur_result.push(deepReadTree(child_param[key]));
            }
        }else{
            cur_result = param.description;
        }

        return cur_result;
    }


Logically speaking, your items should be an array, otherwise your data structure cannot be produced.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template