{"id":223347,"date":"2021-08-06T08:17:49","date_gmt":"2021-08-06T00:17:49","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=223347"},"modified":"2021-07-26T10:18:28","modified_gmt":"2021-07-26T02:18:28","slug":"learn-json-stringify","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/learn-json-stringify.html","title":{"rendered":"\u5b66\u4e60JSON.stringify()"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nJSON(JavaScript Object Notation, JS \u5bf9\u8c61\u7b80\u8c31) \u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u3002\u5b83\u57fa\u4e8e ECMAScript (\u6b27\u6d32\u8ba1\u7b97\u673a\u534f\u4f1a\u5236\u5b9a\u7684js\u89c4\u8303)\u7684\u4e00\u4e2a\u5b50\u96c6\uff0c\u91c7\u7528\u5b8c\u5168\u72ec\u7acb\u4e8e\u7f16\u7a0b\u8bed\u8a00\u7684\u6587\u672c\u683c\u5f0f\u6765\u5b58\u50a8\u548c\u8868\u793a\u6570\u636e\u3002\u7b80\u6d01\u548c\u6e05\u6670\u7684\u5c42\u6b21\u7ed3\u6784\u4f7f\u5f97 JSON \u6210\u4e3a\u7406\u60f3\u7684\u6570\u636e\u4ea4\u6362\u8bed\u8a00\u3002 \u6613\u4e8e\u4eba\u9605\u8bfb\u548c\u7f16\u5199\uff0c\u540c\u65f6\u4e5f\u6613\u4e8e\u673a\u5668\u89e3\u6790\u548c\u751f\u6210\uff0c\u5e76\u6709\u6548\u5730\u63d0\u5347\u7f51\u7edc\u4f20\u8f93\u6548\u7387\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"
\nJSON \u901a\u5e38\u7528\u4e8e\u4e0e\u670d\u52a1\u7aef\u4ea4\u6362\u6570\u636e\u3002\u5728\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u65f6\u4e00\u822c\u662f\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 JSON.stringify() \u65b9\u6cd5\u5c06 JavaScript \u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n

\u8bed\u6cd5<\/strong><\/div>\n
JSON.stringify(value[, replacer[, space]])<\/pre>\n

\u53c2\u6570\u8bf4\u660e\uff1a<\/p>\n

value:\u5fc5\u9700\uff0c \u8981\u8f6c\u6362\u7684 JavaScript \u503c\uff08\u901a\u5e38\u4e3a\u5bf9\u8c61\u6216\u6570\u7ec4\uff09\u3002<\/p>\n

replacer:\u53ef\u9009\u3002\u7528\u4e8e\u8f6c\u6362\u7ed3\u679c\u7684\u51fd\u6570\u6216\u6570\u7ec4\u3002<\/p>\n

\u5982\u679c replacer \u4e3a\u51fd\u6570\uff0c\u5219 JSON.stringify \u5c06\u8c03\u7528\u8be5\u51fd\u6570\uff0c\u5e76\u4f20\u5165\u6bcf\u4e2a\u6210\u5458\u7684\u952e\u548c\u503c\u3002\u4f7f\u7528\u8fd4\u56de\u503c\u800c\u4e0d\u662f\u539f\u59cb\u503c\u3002\u5982\u679c\u6b64\u51fd\u6570\u8fd4\u56de undefined\uff0c\u5219\u6392\u9664\u6210\u5458\u3002\u6839\u5bf9\u8c61\u7684\u952e\u662f\u4e00\u4e2a\u7a7a\u5b57\u7b26\u4e32\uff1a\"\"\u3002<\/p>\n

\u5982\u679c replacer \u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5219\u4ec5\u8f6c\u6362\u8be5\u6570\u7ec4\u4e2d\u5177\u6709\u952e\u503c\u7684\u6210\u5458\u3002\u6210\u5458\u7684\u8f6c\u6362\u987a\u5e8f\u4e0e\u952e\u5728\u6570\u7ec4\u4e2d\u7684\u987a\u5e8f\u4e00\u6837\u3002\u5f53 value \u53c2\u6570\u4e5f\u4e3a\u6570\u7ec4\u65f6\uff0c\u5c06\u5ffd\u7565 replacer \u6570\u7ec4\u3002<\/p>\n

space:\u53ef\u9009\uff0c\u6587\u672c\u6dfb\u52a0\u7f29\u8fdb\u3001\u7a7a\u683c\u548c\u6362\u884c\u7b26\uff0c\u5982\u679c space \u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u5219\u8fd4\u56de\u503c\u6587\u672c\u5728\u6bcf\u4e2a\u7ea7\u522b\u7f29\u8fdb\u6307\u5b9a\u6570\u76ee\u7684\u7a7a\u683c\uff0c\u5982\u679c space \u5927\u4e8e 10\uff0c\u5219\u6587\u672c\u7f29\u8fdb 10 \u4e2a\u7a7a\u683c\u3002space \u4e5f\u53ef\u4ee5\u4f7f\u7528\u975e\u6570\u5b57\uff0c\u5982\uff1a\\t\u3002<\/p>\n

JavaScript \u5bf9\u8c61\u8f6c\u6362<\/strong><\/div>\n

\u4f8b\u5982\u6211\u4eec\u5411\u670d\u52a1\u5668\u53d1\u9001\u4ee5\u4e0b\u6570\u636e\uff1a<\/p>\n

var obj = { \"name\":\"runoob\", \"alexa\":10000, \"site\":\"lrxjmw.cn\"};<\/pre>\n

\u6211\u4eec\u4f7f\u7528 JSON.stringify() \u65b9\u6cd5\u5904\u7406\u4ee5\u4e0a\u6570\u636e\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1a<\/p>\n

var myJSON = JSON.stringify(obj);<\/pre>\n

myJSON \u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u5c06 myJSON \u53d1\u9001\u5230\u670d\u52a1\u5668\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

var obj = { \"name\":\"runoob\", \"alexa\":10000, \"site\":\"lrxjmw.cn\"};\r\nvar myJSON = JSON.stringify(obj);\r\ndocument.getElementById(\"demo\").innerHTML = myJSON;<\/pre>\n
JavaScript \u6570\u7ec4\u8f6c\u6362<\/strong><\/div>\n

\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c06 JavaScript \u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

var arr = [ \"Google\", \"Runoob\", \"Taobao\", \"Facebook\" ];\r\nvar myJSON = JSON.stringify(arr);<\/pre>\n

myJSON \u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u5c06 myJSON \u53d1\u9001\u5230\u670d\u52a1\u5668\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

var arr = [ \"Google\", \"Runoob\", \"Taobao\", \"Facebook\" ];\r\nvar myJSON = JSON.stringify(arr);\r\ndocument.getElementById(\"demo\").innerHTML = myJSON;<\/pre>\n
\u5f02\u5e38<\/strong><\/div>\n

\u89e3\u6790\u6570\u636e
\nJSON \u4e0d\u80fd\u5b58\u50a8 Date \u5bf9\u8c61\u3002<\/p>\n

JSON.stringify() \u4f1a\u5c06\u6240\u6709\u65e5\u671f\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

var obj = { \"name\":\"Runoob\", \"initDate\":new Date(), \"site\":\"lrxjmw.cn\"};\r\nvar myJSON = JSON.stringify(obj);\r\ndocument.getElementById(\"demo\").innerHTML = myJSON;<\/pre>\n

\u4e4b\u540e\u4f60\u53ef\u4ee5\u518d\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a Date \u5bf9\u8c61\u3002<\/p>\n

\u89e3\u6790\u51fd\u6570<\/strong><\/div>\n

JSON \u4e0d\u5141\u8bb8\u5305\u542b\u51fd\u6570\uff0cJSON.stringify() \u4f1a\u5220\u9664 JavaScript \u5bf9\u8c61\u7684\u51fd\u6570\uff0c\u5305\u62ec key \u548c value\u3002<\/p>\n

\u5b9e\u4f8b<\/p>\n

var obj = { \"name\":\"Runoob\", \"alexa\":function () {return 10000;}, \"site\":\"lrxjmw.cn\"};\r\nvar myJSON = JSON.stringify(obj);\r\n \r\ndocument.getElementById(\"demo\").innerHTML = myJSON;<\/pre>\n

\u6211\u4eec\u53ef\u4ee5\u5728\u6267\u884c JSON.stringify() \u51fd\u6570\u524d\u5c06\u51fd\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u6765\u907f\u514d\u4ee5\u4e0a\u95ee\u9898\u7684\u53d1\u751f\uff1a<\/p>\n

\u5b9e\u4f8b<\/p>\n

var obj = { \"name\":\"Runoob\", \"alexa\":function () {return 10000;}, \"site\":\"lrxjmw.cn\"};\r\nobj.alexa = obj.alexa.toString();\r\nvar myJSON = JSON.stringify(obj);\r\n \r\ndocument.getElementById(\"demo\").innerHTML = myJSON;<\/pre>\n","protected":false},"excerpt":{"rendered":"

JSON \u901a\u5e38\u7528\u4e8e\u4e0e\u670d\u52a1\u7aef\u4ea4\u6362\u6570\u636e\u3002\u5728\u5411\u670d\u52a1\u5668\u53d1\u9001\u6570\u636e\u65f6\u4e00\u822c\u662f\u5b57\u7b26\u4e32\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 JSON.stringif […]<\/p>\n","protected":false},"author":1481,"featured_media":223326,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-223347","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1481"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=223347"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223347\/revisions"}],"predecessor-version":[{"id":223353,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/223347\/revisions\/223353"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/223326"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=223347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=223347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=223347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}