json转treeJson

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>

    <body>
        <script type="text/javascript">
            var json = [{
                    "id": 7,
                    "parentId": null,
                    "name": "服装",
                    "goodsType": null,
                    "logo": "1",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 1,
                    "updateOn": null
                }, {
                    "id": 9,
                    "parentId": 7,
                    "name": "大衣",
                    "goodsType": "衣服",
                    "logo": "1",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 1,
                    "updateOn": "2170d40e37a447ac8a7f1ae12a6752c1"
                }, {
                    "id": 13,
                    "parentId": 7,
                    "name": "棉衣",
                    "goodsType": "衣服",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": false,
                    "hotSelling": false,
                    "sortNum": 1,
                    "updateOn": null
                }, {
                    "id": 46,
                    "parentId": 14,
                    "name": "卡西龙",
                    "goodsType": "儿童服装",
                    "logo": "http://bizain01.oss-cn-shenzhen.aliyuncs.com/images/3989002422360997923/42300614d5fc4c5a9c5130329aa5df89",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 1,
                    "updateOn": null
                }, {
                    "id": 8,
                    "parentId": null,
                    "name": "家具",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 2,
                    "updateOn": "1"
                }, {
                    "id": 14,
                    "parentId": 7,
                    "name": "棉衣",
                    "goodsType": "家具,儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png0",
                    "recommend": false,
                    "hotSelling": true,
                    "sortNum": 2,
                    "updateOn": "2170d40e37a447ac8a7f1ae12a6752c1"
                }, {
                    "id": 17,
                    "parentId": 9,
                    "name": "呢子大衣2",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": false,
                    "hotSelling": true,
                    "sortNum": 2,
                    "updateOn": null
                }, {
                    "id": 44,
                    "parentId": 13,
                    "name": "小服装",
                    "goodsType": "儿童服装",
                    "logo": "",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 2,
                    "updateOn": null
                }, {
                    "id": 11,
                    "parentId": null,
                    "name": "超市",
                    "goodsType": null,
                    "logo": "",
                    "recommend": false,
                    "hotSelling": false,
                    "sortNum": 3,
                    "updateOn": null
                }, {
                    "id": 15,
                    "parentId": 7,
                    "name": "测试棉衣",
                    "goodsType": "衣服衣服衣服衣服衣",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": false,
                    "hotSelling": true,
                    "sortNum": 3,
                    "updateOn": null
                }, {
                    "id": 16,
                    "parentId": 7,
                    "name": "儿童服装",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 3,
                    "updateOn": "ec0455cbf07b4244a4ae791c1c96e3a1"
                }, {
                    "id": 20,
                    "parentId": 18,
                    "name": "桌子2",
                    "goodsType": "家具",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 3,
                    "updateOn": null
                }, {
                    "id": 12,
                    "parentId": null,
                    "name": "海淘购",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": false,
                    "hotSelling": false,
                    "sortNum": 4,
                    "updateOn": "1"
                }, {
                    "id": 18,
                    "parentId": 8,
                    "name": "桌子",
                    "goodsType": "家具",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 4,
                    "updateOn": null
                }, {
                    "id": 51,
                    "parentId": 13,
                    "name": "大服装",
                    "goodsType": "成人服装",
                    "logo": "http://bizain01.oss-cn-shenzhen.aliyuncs.com/images/3989002422360997924/328bbecf5571482a885c7d329958d89f",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 5,
                    "updateOn": null
                }, {
                    "id": 52,
                    "parentId": 8,
                    "name": "具",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/wxapp/image/BWT.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 6,
                    "updateOn": null
                }, {
                    "id": 23,
                    "parentId": 7,
                    "name": "服装1",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 7,
                    "updateOn": null
                }, {
                    "id": 29,
                    "parentId": null,
                    "name": "test1",
                    "goodsType": null,
                    "logo": "",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 7,
                    "updateOn": null
                }, {
                    "id": 53,
                    "parentId": 8,
                    "name": "测试家具",
                    "goodsType": null,
                    "logo": "https://bizain.net/wxapp/image/BWT.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 7,
                    "updateOn": null
                }, {
                    "id": 24,
                    "parentId": 7,
                    "name": "服装2",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 8,
                    "updateOn": null
                }, {
                    "id": 30,
                    "parentId": null,
                    "name": "test2",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/manager/api/file/download/85279b8dda534cd1ab821ac908c1e2d6.jpg",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 8,
                    "updateOn": null
                }, {
                    "id": 54,
                    "parentId": 8,
                    "name": "小家具",
                    "goodsType": "儿童服装,成人服装,测试类型",
                    "logo": "https://bizain.net/wxapp/image/BWT.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 8,
                    "updateOn": null
                }, {
                    "id": 25,
                    "parentId": 7,
                    "name": "服装3",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 9,
                    "updateOn": null
                }, {
                    "id": 31,
                    "parentId": null,
                    "name": "test3",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/manager/api/file/download/0bbc518300b745e6ad0a38ab57f4749b.jpg",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 9,
                    "updateOn": null
                }, {
                    "id": 43,
                    "parentId": 9,
                    "name": "呢子大衣6",
                    "goodsType": "儿童服装",
                    "logo": "",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 9,
                    "updateOn": null
                }, {
                    "id": 26,
                    "parentId": 7,
                    "name": "服装4",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 10,
                    "updateOn": null
                }, {
                    "id": 32,
                    "parentId": null,
                    "name": "test5",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/manager/api/file/download/bbcc0d42984344558861c2ccbf3e7416.jpg",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 10,
                    "updateOn": null
                }, {
                    "id": 42,
                    "parentId": 9,
                    "name": "呢子大衣5",
                    "goodsType": "儿童服装",
                    "logo": "",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 10,
                    "updateOn": null
                }, {
                    "id": 27,
                    "parentId": 7,
                    "name": "服装5",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 11,
                    "updateOn": null
                }, {
                    "id": 33,
                    "parentId": null,
                    "name": "test6",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/manager/api/file/download/4b782b55bfc14418a8f2173d23b304c5.jpg",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 11,
                    "updateOn": null
                }, {
                    "id": 41,
                    "parentId": 9,
                    "name": "呢子大衣4",
                    "goodsType": "儿童服装",
                    "logo": "",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 11,
                    "updateOn": null
                }, {
                    "id": 28,
                    "parentId": 7,
                    "name": "服装6",
                    "goodsType": "儿童服装",
                    "logo": "https://bizain.net/test/api/platform/business/api/file/download/abyo.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 12,
                    "updateOn": null
                }, {
                    "id": 34,
                    "parentId": null,
                    "name": "test7",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/manager/api/file/download/5ed6f6922a0640179e948aac1ae3aa04.jpg",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 12,
                    "updateOn": null
                }, {
                    "id": 40,
                    "parentId": 9,
                    "name": "呢子大衣3",
                    "goodsType": "儿童服装",
                    "logo": "",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 12,
                    "updateOn": null
                }, {
                    "id": 35,
                    "parentId": null,
                    "name": "test8",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/manager/api/file/download/3c7f3b035f7944a59e1aa6849cf20699.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 13,
                    "updateOn": null
                }, {
                    "id": 38,
                    "parentId": 7,
                    "name": "服装7",
                    "goodsType": "儿童服装",
                    "logo": "",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 13,
                    "updateOn": null
                }, {
                    "id": 39,
                    "parentId": 9,
                    "name": "呢子大衣1",
                    "goodsType": "儿童服装",
                    "logo": "",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 13,
                    "updateOn": null
                }, {
                    "id": 36,
                    "parentId": null,
                    "name": "test9",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/manager/api/file/download/42a982e885ac4d158cb2ced9dbb39669.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 14,
                    "updateOn": null
                }, {
                    "id": 37,
                    "parentId": null,
                    "name": "test10",
                    "goodsType": null,
                    "logo": "https://bizain.net/test/api/platform/manager/api/file/download/a5cc07c9cbce49af8dd02ef79b1af266.png",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 18,
                    "updateOn": null
                }, {
                    "id": 48,
                    "parentId": 21,
                    "name": "三鹿",
                    "goodsType": null,
                    "logo": "http://bizain01.oss-cn-shenzhen.aliyuncs.com/images/3989002422360997923/3b0f9e80bbc440bc976f8aa019e5537a",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 66,
                    "updateOn": null
                }, {
                    "id": 50,
                    "parentId": 22,
                    "name": "小白龙",
                    "goodsType": null,
                    "logo": "http://bizain01.oss-cn-shenzhen.aliyuncs.com/images/3989002422360997923/78a4e946f593477ca897e2d2a7ed96df",
                    "recommend": false,
                    "hotSelling": true,
                    "sortNum": 80,
                    "updateOn": null
                }, {
                    "id": 49,
                    "parentId": 15,
                    "name": "阿玛尼",
                    "goodsType": "儿童服装",
                    "logo": "http://bizain01.oss-cn-shenzhen.aliyuncs.com/images/3989002422360997923/cbfa8e4e4b8b4cc4b99cea9280e10acf",
                    "recommend": true,
                    "hotSelling": true,
                    "sortNum": 99,
                    "updateOn": null
                }]
            var newjson = [];
            for(var i = 0; i < json.length; i++) {
                if(!json[i].parentId) newjson.push(json[i]);
                for(var j = i + 1; j < json.length; j++) {
                    if(json[i].id == json[j].parentId) {
                        json[i].children = json[i].children || [];
                        json[i].children.push(json[j]);
                    } else if(json[j].id == json[i].parentId) {
                        json[j].children = json[j].children || [];
                        json[j].children.push(json[i]);
                    }
                }
            }
            console.log(newjson)
        </script>
    </body>

</html>
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 211,743评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,296评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,285评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,485评论 1 283
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,581评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,821评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,960评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,719评论 0 266
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,186评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,516评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,650评论 1 340
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,329评论 4 330
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,936评论 3 313
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,757评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,991评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,370评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,527评论 2 349

推荐阅读更多精彩内容