js输出json数组 按树形结构输出

正文开始

<pre>var sonString = '{"groupId":["a","b","c","d"],"from_uid":150,"to_uid":147,"time":1542089617,"data":{"method":"memberMsg","msg":{"action":"v3","fromUid":"150","gid":1362,"gname":"哈哈哈回家H是多大的","nickname":"伊伽奈义德","content":"[EM^0043][EM^0043][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035][EM^0035]","type":1,"fromTime":1542089617,"uniqueId":"1542089617905768","isReTry":0,"noticeSwitch":0}}}'; var objs = JSON.parse(sonString); objs; var str_ = ''; var jiexiObj = function (obj_, level, spaceParent) { var spac1 = new Array(level).join(' '); var spac2 = spac1 + new Array(7).join(' '); var __str = '', frontStr, endStr; if (typeof obj_[0] != 'undefined') { console.log(obj_[0]); frontStr = '['; endStr = ']'; } else { frontStr = '{'; endStr = '}' } __str += spac1 + frontStr + '<br>'; $.each(obj_, function (index, v) { if (isNaN(index)) { __str += spac2 + spaceParent + '"' + index + '" => '; } else { __str += spac2 + spaceParent + '[' + index + '] => '; } if (typeof v == 'object') { __str += jiexiObj(v, (level + 1), spac1 + spac2) + ',<br>'; } else { __str += v + ',<br>'; } }); __str += spac1 + spac2 + spaceParent + endStr + ''; return __str; }; str_ = jiexiObj(objs, 0, ''); //str_; msgWin('查看json', str_); </pre>

正文结束

js 正则替换返回值做回调函数 jsbug 谷歌浏览器底下 首次加载页面时 如果执行了ajax请求会导致初始化的settimeout失败 必须要有一个空的settimeout在前面垫底。