JSONView

在gitlab上面,有一个jQuery JSONView插件,地址为:https://github.com/yesmeck/jquery-jsonview

demo地址:http://yesmeck.github.io/jquery-jsonview/

jQuery格式化显示json数据

 

注意:部分key前面有一个减号,点击减号,就可以收缩了。点击加号,可以展开。

但是这样有一个问题,我需要用鼠标copy时,会带有减号。复制之后,就是一个错误的数据!!!

 

jquery.json-viewer.js

下载地址为:http://www.jq22.com/jquery-info13551

demo地址:http://www.jq22.com/yanshi13551

注意:下载需要登录jq22.com账号

效果如下:

jQuery格式化显示json数据

这个才是我们想要的效果,注意:它有竖条,可以方便查看层级关系。

而且copy数据时,也不会带有多余的符号。点击三角形符号,也可以方便收缩和展开!!

 

需求

有这样一个需求,我用django开发一个接口,需要给其他人员展示数据。展示数据时,默认直接展开json 格式化好的数据,方便其他开发人员调用。

但是jq22.com 提供的插件,有一个textarea输入框,我需要把它给去掉。

默认json格式化的数据中,key是没有带双引号的,我需要默认勾选它,因此要修改js代码。

 

二、修改插件代码

基于上面的2点需求,下载jq22.com 提供的插件后,解压代码。

修改index.html,完整代码如下:

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jQuery查看json格式数据插件</title>
    <link rel="stylesheet" type="text/css" href="http://www.jq22.com/jquery/bootstrap-3.3.4.css">
    <link href="css/jquery.json-viewer.css" type="text/css" rel="stylesheet"/>
    <style>
        body {
            background-color: #F7F7F7
        }
    </style>
</head>
<body>
<div class="jq22-container">
    <div class="container" style="margin-top: 1em;">
        <div class="row">
            <pre id="json-renderer"></pre>
        </div>
    </div>
</div>
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="js/jquery.json-viewer.js"></script>
<script type="text/javascript">
    $(function () {
        // json数据
        var json = {
            "id": 1001,
            "type": "donut",
            "name": "Cake",
            "description": "http://www.jq22.com",
            "price": 2.55,
            "available": {
                store: 42,
                warehouse: 600
            },
            "topping": [
                {"id": 5001, "type": "None"},
                {"id": 5002, "type": "Glazed"},
                {"id": 5005, "type": "Sugar"},
                {"id": 5003, "type": "Chocolate"},
                {"id": 5004, "type": "Maple"}
            ]
        };

        //格式化json
        try {
            var input = eval('(' + JSON.stringify(json) + ')');
        } catch (error) {
            return alert("Cannot eval JSON: " + error);
        }
        var options = {
            //为Key添加双引号
            withQuotes: true
        };
        $('#json-renderer').jsonViewer(input, options);
    });
</script>
</body>
</html>
View Code

相关文章:

  • 2022-12-23
  • 2021-09-10
  • 2021-07-05
  • 2021-11-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-15
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案