小白教程
所有教程
关于
Search
172.70.178.233
172.70.178.233
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Javascript json stringify”的源代码
本页内容
上一节:
Javascript_json_parse
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:JSON.stringify()}}[[Category:js json|2]] = JavaScript JSON.stringify() = [[js json|JavaScript JSON]] JSON.stringify() 方法用于将 JavaScript 值转换为 JSON 字符串。 === 语法 === <pre> JSON.stringify(value[, replacer[, space]]) </pre> '''参数说明:''' <p>'''value:'''</p> <p>必需, 要转换的 JavaScript 值(通常为对象或数组)。</p> <p>'''replacer:'''</p> <p>可选。用于转换结果的函数或数组。</p> <p>如果 replacer 为函数,则 JSON.stringify 将调用该函数,并传入每个成员的键和值。使用返回值而不是原始值。如果此函数返回 undefined,则排除成员。根对象的键是一个空字符串:""。</p> <p>如果 replacer 是一个数组,则仅转换该数组中具有键值的成员。成员的转换顺序与键在数组中的顺序一样。</p> <p>'''space:'''</p> <p>可选,文本添加缩进、空格和换行符,如果 space 是一个数字,则返回值文本在每个级别缩进指定数目的空格,如果 space 大于 10,则文本缩进 10 个空格。space 也可以使用非数字,如:\t。</p> === 返回值: === 返回包含 JSON 文本的字符串。 == == <sample title="" desc="" lang="html" hererun="1"> var str = {"name":"小白教程", "site":"http://www.xiaobai.wang"} 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 用于格式化输出 </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> </head> <body> <p id="demo"></p> <script> var str = {"name":"小白教程", "site":"http://www.xiaobai.wang"} 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> </run> [[js json|JavaScript JSON]]
返回至“
Javascript json stringify
”。
上一节:
Javascript_json_parse