打平json成为数组

var _json = {
        "children": [{
            "children": [{
                "children": [{
                    "children": [],
                    "content": "",
                    "distance": 0,
                    "id": 350,
                    "order": "1",
                    "par_id": 318,
                    "paragraph_id": 470,
                    "text": "新5",
                    "state": "open",
                    "domId": "_easyui_tree_14",
                    "target": {"pageY": 146}
                }, {
                    "children": [],
                    "content": "",
                    "distance": 0,
                    "id": 320,
                    "order": "2",
                    "par_id": 6,
                    "paragraph_id": 440,
                    "text": "进攻打法",
                    "state": "open",
                    "domId": "_easyui_tree_4"
                }, {
                    "children": [],
                    "content": "",
                    "distance": 0,
                    "id": 321,
                    "order": "3",
                    "par_id": 6,
                    "paragraph_id": 441,
                    "text": "中场打法",
                    "state": "open",
                    "domId": "_easyui_tree_5"
                }],
                "content": "",
                "distance": 0,
                "id": 6,
                "order": "(二).",
                "par_id": 4,
                "paragraph_id": 230,
                "text": "门诊工作",
                "state": "open",
                "domId": "_easyui_tree_3",
                "target": {"jQuery11130507155963828726": 50}
            }, {
                "children": [],
                "content": "",
                "distance": 0,
                "id": 7,
                "order": "(三).",
                "par_id": 4,
                "paragraph_id": 240,
                "text": "医技科室",
                "state": "open",
                "domId": "_easyui_tree_6"
            }, {
                "children": [{
                    "children": [],
                    "content": "",
                    "distance": 0,
                    "id": 351,
                    "order": "2",
                    "par_id": 318,
                    "paragraph_id": 471,
                    "text": "新1",
                    "state": "open",
                    "domId": "_easyui_tree_9"
                }, {
                    "children": [],
                    "content": "",
                    "distance": 0,
                    "id": 352,
                    "order": "3",
                    "par_id": 318,
                    "paragraph_id": 472,
                    "text": "示范点",
                    "state": "open",
                    "domId": "_easyui_tree_10"
                }, {
                    "children": [],
                    "content": "",
                    "distance": 0,
                    "id": 353,
                    "order": "4",
                    "par_id": 318,
                    "paragraph_id": 473,
                    "text": "发生大",
                    "state": "open",
                    "domId": "_easyui_tree_11"
                }, {
                    "children": [],
                    "content": "",
                    "distance": 0,
                    "id": 354,
                    "order": "5",
                    "par_id": 318,
                    "paragraph_id": 474,
                    "text": "选择",
                    "state": "open",
                    "domId": "_easyui_tree_12"
                }],
                "content": "",
                "distance": 0,
                "id": 318,
                "order": "(四).",
                "par_id": 4,
                "paragraph_id": 438,
                "text": "发生大",
                "state": "open",
                "domId": "_easyui_tree_7",
                "target": {"jQuery11130507155963828726": 66, "entered": false}
            }],
            "content": "",
            "distance": 0,
            "id": 4,
            "order": "一.",
            "par_id": 1,
            "paragraph_id": 230,
            "text": "医疗质控工作小结",
            "state": "open",
            "domId": "_easyui_tree_2",
            "target": {"jQuery11130507155963828726": 46}
        }, {
            "children": [],
            "content": "",
            "distance": 0,
            "id": 8,
            "order": "二.",
            "par_id": 1,
            "paragraph_id": 230,
            "text": "整改意见",
            "state": "open",
            "domId": "_easyui_tree_13"
        }],
        "content": "",
        "distance": 0,
        "id": 1,
        "order": "",
        "par_id": 0,
        "paragraph_id": 240,
        "text": "目录",
        "state": "open",
        "domId": "_easyui_tree_1",
        "target": {"jQuery11130507155963828726": 42}
    };

 function Init(obj){
        this.text=obj.text;
        this.id=obj.id;
        this.order=obj.order;
        this.children=[];
        this.par_id=obj.obj;
        this.paragraph_id=obj.paragraph_id;
        this.distance=obj.distance;
        this.content=obj.content;
    }

function recursion(obj, tempObj,_arr) {
        if(obj.children.length>0){
            var addArr=[];
            for (var i = 0; i < obj.children.length; i++) {
                !function (i) {
                    addArr.push(obj.children[i])
                }(i)
                if (i==obj.children.length-1) {/*添加完*/
                    tempObj=addArr.concat(tempObj);
                    var temp = JSON.stringify(tempObj[0]);
                        temp=JSON.parse(temp);

                    var item = {
                        "text": temp.text,
                        "id": temp.id,
                        "order": temp.order,
                        "children": [],
                    }
                    _arr.push( new Init(temp));
                    var del=tempObj.shift();/*删掉头部*/
                    if(del.children.length>0){
                        recursion(del, tempObj, _arr)
                    }else{
                        for (var j = 0,leg=tempObj.length; j 0){
                                var  _del= tempObj.shift();/*删掉头部*/
                                _del = JSON.stringify(_del);
                                _del=JSON.parse(_del);
                                _arr.push( new Init(_del));
                                recursion(_del, tempObj, _arr);
                                break;
                            }else{
                                var  _del= tempObj.shift();/*删掉头部*/
                                _del = JSON.stringify(_del);
                                _del=JSON.parse(_del);
                                _arr.push( new Init(_del));
                            }
                        }

                    }

                }
            }
        }else{
            return ;
        }
    }
var _arrs = [] ,workSpace=[];
    _arrs.push( new Init(_json));
 recursion(_json, workSpace,_arrs);

你可能感兴趣的:(打平json成为数组)