码迷,mamicode.com
首页 > Web开发 > 详细

json转树状结构

时间:2017-06-21 09:40:09      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:结构   tree   技术   hash   turn   com   海尔   func   eval   

原数据格式

var jsonData = eval(‘[{"id":"4","pid":"1","name":"大家电"},  {"id":"5","pid":"1","name":"生活电器"},  {"id":"1","pid":"0","name":"家用电器"},  {"id":"2","pid":"0","name":"服饰"},  {"id":"3","pid":"0","name":"化妆"},  {"id":"7","pid":"4","name":"空调"}, {"id":"8","pid":"4","name":"冰箱"},  {"id":"9","pid":"4","name":"洗衣机"},  {"id":"10","pid":"4","name":"热水器"},  {"id":"11","pid":"3","name":"面部护理"},  {"id":"12","pid":"3","name":"口腔护理"},  {"id":"13","pid":"2","name":"男装"},  {"id":"14","pid":"2","name":"女装"},  {"id":"15","pid":"7","name":"海尔空调"},  {"id":"16","pid":"7","name":"美的空调"}, {"id":"19","pid":"5","name":"加湿器"},  {"id":"20","pid":"5","name":"电熨斗"}   ]‘);

定义js方法:

function transData(a, idStr, pidStr, chindrenStr){    
                var r = [], hash = {}, id = idStr, pid = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length;    
                for(; i < len; i++){    
                    hash[a[i][id]] = a[i];    
                }    
                for(; j < len; j++){    
                    var aVal = a[j], hashVP = hash[aVal[pid]];    
                    if(hashVP){    
                        !hashVP[children] && (hashVP[children] = []);    
                        hashVP[children].push(aVal);    
                    }else{    
                        r.push(aVal);    
                    }    
                }    
                return r;    
            }  

调用js方法及转换的数据格式

var jsonDataTree = transData(jsonData, ‘id‘, ‘pid‘, ‘chindren‘);

技术分享

json转树状结构

标签:结构   tree   技术   hash   turn   com   海尔   func   eval   

原文地址:http://www.cnblogs.com/123fang/p/7057706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!