zl程序教程

您现在的位置是:首页 >  后端

当前栏目

js 语法:JSON.stringify(data, null, 4)

JSJSONJSON 语法 Data null stringify
2023-09-14 09:01:59 时间

JSON.stringify(data, null, 4)

JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。

JSON.stringify(data, null, 4)
JSON.stringify从一个对象解析出字符串,第三个参数是格式化缩进格式按照四个字符缩进
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>

<p id="demo"></p>
<script>
var str = {"name":"菜鸟教程", "site":"http://www.runoob.com"}
str_pretty1 = JSON.stringify(str)
document.write( "只有一个参数情况:" );
document.write( "<br>" );
document.write("<pre>" + str_pretty1 + "</pre>" );
document.write( "<br>" );
str_pretty2 = JSON.stringify(str, null, 4) //使用四个空格缩进
document.write( "使用参数情况:" );
document.write( "<br>" );
document.write("<pre>" + str_pretty2 + "</pre>" ); // pre 用于格式化输出
</script>

</body>
</html>