//生成树状json格式 //a=>json数据源 //idstr=>子Id关键字 //pidStr=>父Id关键字 //chindrenStr=>生成的chindrenStr字段 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; }