JavaScript格式化输出并展示Json对象

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_39198406/article/details/84789515

效果就是下面这样子,开始还以为代码很复杂,实际上一句JSON.stringify(data, null, "\t")就解决了。
在这里插入图片描述
具体代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
    <script>
        $(function () {
            var data = {
              name: "Apple",
              age: "18",
              location: "Beijing"
            };
            $("#search_one_result").val(JSON.stringify(data, null, "\t"));
            $("#search_one_result").val(JSON.stringify(data, null, 4));
        });
    </script>
</head>
<body>
<textarea id="search_one_result" cols="50" rows="10" style="margin-top: 30px"></textarea>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_39198406/article/details/84789515