<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="../jquery-1.8.3.min.js"></script>
</head>
<body>
    <div class="wrap">
        <div class="content"></div>
        <div class="text"></div>
        <div class="json"></div>
    </div>
    <p id="show_p">{ "name": "Brett", "address":"北京路23号", "email": "[email protected]" }</p>
    <pre id="out_pre"></pre>

    <script type="text/javascript">
      var text = document.getElementById('show_p').innerText; //获取json格式内容
        var result = JSON.stringify(JSON.parse(text), null, 2);//将字符串转换成json对象
        document.getElementById('out_pre').innerText= result ;
      var students = new Array();
      students[0] = "pika";
      students[1] = "hello";
      students[2] = "hey";
      var stu= new Array();
      stu[0] = "how";
      stu[1] = "are";
      stu[2] = "u";
      var json = JSON.stringify(students,stu);
      alert(json);
    </script>
</body>
<script>
    $(function () {
        $.post("http://pro.djc888.com/wxp/qr/v/list")
            .done(function (data) {
                // console.log(data)
                var i = Math.floor((Math.random() * data.data.length));
                var arr = data.data[i].imgPath;
                var Tarr = data.data[i].weChat;
                var url = 'http://pro.djc888.com/wxp/';
                var img = $("<img src='' >").attr('src', url + arr);
                $('.content').append(img);
                $('.text').html(Tarr);

                $('.json').html(JSON.stringify(data,null,4));
            });

            //
            console.log(data);

    })

    </script>
</html>

总结:JSON.stringify()函数是用来序列化对象的,无论输入什么,输出的都是字符串类型,可以按自己的需求来定制如何序列化,输出结果是怎样的格式

02-13 04:20