<p>需要实现这个一个功能,就是查看详细的json代码,最好是实现json代码的折叠与展示功能,在页面弹出层用到了layui中的layer组件,在组件中展示的内容就是json格式的代码,实例如下所示:</p><pre class="brush:html;toolbar:false"><!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>JQuery JSONView</title> <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.5.5/css/layui.css" /> <link rel="stylesheet" href="./jquery.jsonview.css" /> <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> <script type="text/javascript" src="https://www.layuicdn.com/layui/layui.js"></script> <script type="text/javascript" src="./jquery.jsonview.js"></script> </head> <body> <div id="json-view"></div> </body> <script> layui.use(['layer'], function() { var layer = layui.layer,json = { "name": "潘旭博客", "anumber": 243, "anobject": { "whoa": "nuts", "anarray": [1, 2, "thr<h1>ee"], "more": "stuff" }, "link": "https://panxu.net", "multiline": ['Much like me, you make your way forward,', 'Walking with downturned eyes.', 'Well, I too kept mine lowered.', 'Passer-by, stop here, please.' ].join("\n") }; layer.open({ type: 1, area: ['700px', '450px'], fixed: false, content: $("#json-view"), success: function () { $("#json-view").JSONView(json,{ collapsed: true, nl2br: true, recursive_collapser: true }); }, end: function () { $("#json-view").html(""); } }); }); </script> </html></pre><p>运行效果如下:</p><p><p style="text-align: center;"><img src="/upload/content/20191029/1572342235639646.png" alt="Layui结合JSONView插件实现json格式化"title="Layui结合JSONView插件实现json格式化" width="550px"></p>