JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。
- JSON.stringify(value[, replacer[, space]])
参数说明:
返回包含 JSON 文本的字符串。
- var str = {"name":"城东书院", "site":"http://www.cdsy.xyz"}
- 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 用于格式化输出