json 前段页面展示 格式化 排版
程序员文章站
2022-06-29 14:31:32
...
function ajaxPost() { $.ajax({ url: '${ctx}/json/request.do', type: 'POST', success: function (result) { console.log(result) var html = formatJson(JSON.stringify(result)); console.log(html); $('#show_json').empty(); $('#show_json').html('<pre class="layui-code">' +html+'</pre>'); } }) } function formatJson(json) { var i = 0, len = 0, tab = " ", targetJson = "", indentLevel = 0, inString = false, currentChar = null; for (i = 0, len = json.length; i < len; i += 1) { currentChar = json.charAt(i); switch (currentChar) { case '{': case '[': if (!inString) { targetJson += currentChar + "\n" + repeat(tab, indentLevel + 1); indentLevel += 1; } else { targetJson += currentChar; } break; case '}': case ']': if (!inString) { indentLevel -= 1; targetJson += "\n" + repeat(tab, indentLevel) + currentChar; } else { targetJson += currentChar; } break; case ',': if (!inString) { targetJson += ",\n" + repeat(tab, indentLevel); } else { targetJson += currentChar; } break; case ':': if (!inString) { targetJson += ": "; } else { targetJson += currentChar; } break; case ' ': case "\n": case "\t": if (inString) { targetJson += currentChar; } break; case '"': if (i > 0 && json.charAt(i - 1) !== '\\') { inString = !inString; } targetJson += currentChar; break; default: targetJson += currentChar; break; } } return targetJson; } function repeat(s, count) { return new Array(count + 1).join(s); }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/
谢谢您的赞助,我会做的更好!
推荐阅读
-
开发谷歌浏览器插件会上瘾,搞了一个JSONViewer,一个页面格式化多条JSON,提升工作效率
-
json 格式化展示 编辑(第三方开源框架 FlexiJsonEditor)
-
json 前段页面展示 格式化 排版
-
js格式化 GBK的页面输出JSON格式的php函数
-
基于javascript的JSON格式页面展示美化方法_javascript技巧
-
web页面数据展示新想法(json)_json
-
基于javascript的JSON格式页面展示美化方法_javascript技巧
-
js格式化 GBK的页面输出JSON格式的php函数
-
web页面数据展示新想法(json)_json
-
开发谷歌浏览器插件会上瘾,搞了一个JSONViewer,一个页面格式化多条JSON,提升工作效率