{"id":262797,"date":"2023-02-16T10:56:16","date_gmt":"2023-02-16T02:56:16","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=262797"},"modified":"2023-01-30T11:57:26","modified_gmt":"2023-01-30T03:57:26","slug":"json-linux-java","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/json-linux-java.html","title":{"rendered":"JS JSON.stringify()\u76845\u4e2a\u4f7f\u7528\u573a\u666f\u8be6\u89e3"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nJSON.stringify()\u65b9\u6cd5\u7528\u4e8e\u5c06\u4e00\u4e2a\u503c\u8f6c\u4e3aJSON\u5b57\u7b26\u4e32,\u8be5\u5b57\u7b26\u4e32\u7b26\u5408JSON\u683c\u5f0f,\u5e76\u4e14\u53ef\u4ee5\u88abJSON.parse()\u65b9\u6cd5\u8fd8\u539f,\u4e0b\u9762\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8eJS JSON.stringify()\u76845\u4f7f\u7528\u573a\u666f,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u524d\u8a00<\/strong><\/div>\n

JSON.stringify() \u65b9\u6cd5\u5c06\u4e00\u4e2a JavaScript \u5bf9\u8c61\u6216\u503c\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u5982\u679c\u6307\u5b9a\u4e86\u4e00\u4e2a replacer \u51fd\u6570\uff0c\u5219\u53ef\u4ee5\u9009\u62e9\u6027\u5730\u66ff\u6362\u503c\uff0c\u6216\u8005\u6307\u5b9a\u7684 replacer \u662f\u6570\u7ec4\uff0c\u5219\u53ef\u9009\u62e9\u6027\u5730\u4ec5\u5305\u542b\u6570\u7ec4\u6307\u5b9a\u7684\u5c5e\u6027\u3002<\/p>\n

\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

JSON.stringify(value[, replacer [, space]])<\/pre>\n
  • \u7b2c\u4e00\u4e2a\u53c2\u6570 value\uff1a\u5c06\u8981\u5e8f\u5217\u5316\u6210 \u4e00\u4e2a JSON \u5b57\u7b26\u4e32\u7684\u503c\u3002<\/li>\n
  • \u7b2c\u4e8c\u4e2a\u53c2\u6570 replacer\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u5982\u679c\u8be5\u53c2\u6570\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u5219\u5728\u5e8f\u5217\u5316\u8fc7\u7a0b\u4e2d\uff0c\u88ab\u5e8f\u5217\u5316\u7684\u503c\u7684\u6bcf\u4e2a\u5c5e\u6027\u90fd\u4f1a\u7ecf\u8fc7\u8be5\u51fd\u6570\u7684\u8f6c\u6362\u548c\u5904\u7406\uff1b\u5982\u679c\u8be5\u53c2\u6570\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u5219\u53ea\u6709\u5305\u542b\u5728\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u7684\u5c5e\u6027\u540d\u624d\u4f1a\u88ab\u5e8f\u5217\u5316\u5230\u6700\u7ec8\u7684 JSON \u5b57\u7b26\u4e32\u4e2d\uff1b\u5982\u679c\u8be5\u53c2\u6570\u4e3a null \u6216\u8005\u672a\u63d0\u4f9b\uff0c\u5219\u5bf9\u8c61\u6240\u6709\u7684\u5c5e\u6027\u90fd\u4f1a\u88ab\u5e8f\u5217\u5316\u3002<\/li>\n
  • \u7b2c\u4e09\u4e2a\u53c2\u6570 space\uff1a\u53ef\u9009\u53c2\u6570\uff0c\u6307\u5b9a\u7f29\u8fdb\u7528\u7684\u7a7a\u767d\u5b57\u7b26\u4e32\uff0c\u7528\u4e8e\u7f8e\u5316\u8f93\u51fa\uff08pretty-print\uff09\uff1b\u5982\u679c\u53c2\u6570\u662f\u4e2a\u6570\u5b57\uff0c\u5b83\u4ee3\u8868\u6709\u591a\u5c11\u7684\u7a7a\u683c\uff1b\u4e0a\u9650\u4e3a 10\u3002\u8be5\u503c\u82e5\u5c0f\u4e8e 1\uff0c\u5219\u610f\u5473\u7740\u6ca1\u6709\u7a7a\u683c\uff1b\u5982\u679c\u8be5\u53c2\u6570\u4e3a\u5b57\u7b26\u4e32\uff08\u5f53\u5b57\u7b26\u4e32\u957f\u5ea6\u8d85\u8fc7 10 \u4e2a\u5b57\u6bcd\uff0c\u53d6\u5176\u524d 10 \u4e2a\u5b57\u6bcd\uff09\uff0c\u8be5\u5b57\u7b26\u4e32\u5c06\u88ab\u4f5c\u4e3a\u7a7a\u683c\uff1b\u5982\u679c\u8be5\u53c2\u6570\u6ca1\u6709\u63d0\u4f9b\uff08\u6216\u8005\u4e3a null\uff09\uff0c\u5c06\u6ca1\u6709\u7a7a\u683c\u3002<\/li>\n
    \u7b2c\u4e8c\u4e2a\u53c2\u6570replacer \u4e3a\u6570\u7ec4<\/strong><\/div>\n

    \u662f\u7684\uff0cJSON.stringify() \u51fd\u6570\u53ef\u4ee5\u6709\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5b83\u662f\u8981\u5728\u63a7\u5236\u53f0\u4e2d\u6253\u5370\u7684\u5bf9\u8c61\u7684\u952e\u6570\u7ec4\u3002\u4e0b\u9762\u6765\u770b\u770b\u793a\u4f8b\uff1a<\/p>\n

    const arrayData = [\r\n    {\r\n        id: \"0001\",\r\n        type: \"donut\",\r\n        name: \"Cake\",\r\n        ppu: 0.55,\r\n        batters: {\r\n            batter: [\r\n                { id: \"1001\", type: \"Regular\" },\r\n                { id: \"1002\", type: \"Chocolate\" },\r\n                { id: \"1003\", type: \"Blueberry\" },\r\n                { id: \"1004\", type: \"Devil's Food\" },\r\n            ],\r\n        },\r\n        topping: [\r\n            { id: \"5001\", type: \"None\" },\r\n            { id: \"5002\", type: \"Glazed\" },\r\n            { id: \"5005\", type: \"Sugar\" },\r\n            { id: \"5007\", type: \"Powdered Sugar\" },\r\n            { id: \"5006\", type: \"Chocolate with Sprinkles\" },\r\n            { id: \"5003\", type: \"Chocolate\" },\r\n            { id: \"5004\", type: \"Maple\" },\r\n        ],\r\n    },\r\n];\r\nconsole.log(JSON.stringify(arrayData, [\"name\"])); \/\/ [{\"name\":\"Cake\"}]<\/pre>\n

    \u53ef\u4ee5\u901a\u8fc7\u5728\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4e2d\u5c06\u5176\u4f5c\u4e3a\u6570\u7ec4\u4f20\u9012\u4ec5\u9700\u8981\u6253\u5370\u7684\u952e\uff0c\u800c\u4e0d\u9700\u8981\u6253\u5370\u6574\u4e2a JSON \u5bf9\u8c61\u3002<\/p>\n

    \u7b2c\u4e8c\u4e2a\u53c2\u6570replacer \u4e3a\u51fd\u6570<\/strong><\/div>\n

    \u8fd8\u53ef\u4ee5\u5c06\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4f5c\u4e3a\u51fd\u6570\u4f20\u9012\uff0c\u6839\u636e\u51fd\u6570\u4e2d\u7f16\u5199\u7684\u903b\u8f91\u8bc4\u4f30\u6bcf\u4e2a\u952e\u503c\u5bf9\u3002\u5982\u679c\u8fd4\u56de undefined \u952e\u503c\u5bf9\u5c06\u4e0d\u4f1a\u6253\u5370\u3002\u8bf7\u770b\u4e0b\u9762\u793a\u4f8b\uff1a<\/p>\n

    const user = {\r\n    name: \"DevPoint\",\r\n    age: 35,\r\n};\r\n \r\nconst result = JSON.stringify(user, (key, value) =>\r\n    typeof value === \"string\" ? undefined : value\r\n);\r\nconsole.log(result); \/\/ {\"age\":35}<\/pre>\n

    \u4e0a\u8ff0\u4ee3\u7801\u7684\u8f93\u51fa\uff0c\u53ef\u4ee5\u7528\u6765\u8fc7\u6ee4 JSON \u6570\u636e\u7684\u5c5e\u6027\u503c\u3002<\/p>\n

    \u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a Number<\/strong><\/div>\n

    \u7b2c\u4e09\u4e2a\u53c2\u6570\u63a7\u5236\u6700\u7ec8\u5b57\u7b26\u4e32\u4e2d\u7684\u95f4\u8ddd\u3002\u5982\u679c\u53c2\u6570\u662f\u4e00\u4e2a\u6570\u5b57\uff0c\u5219\u5b57\u7b26\u4e32\u5316\u4e2d\u7684\u6bcf\u4e2a\u7ea7\u522b\u90fd\u5c06\u7f29\u8fdb\u6b64\u6570\u91cf\u7684\u7a7a\u683c\u5b57\u7b26\u3002<\/p>\n

    const user = {\r\n    name: \"DevPoint\",\r\n    age: 35,\r\n    address: {\r\n        city: \"Shenzhen\",\r\n    },\r\n};\r\n \r\nconsole.log(JSON.stringify(user, null, 4));<\/pre>\n

    \u8f93\u51fa\u6253\u5370\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

    {\r\n    \"name\": \"DevPoint\",\r\n    \"age\": 35,\r\n    \"address\": {\r\n        \"city\": \"Shenzhen\"\r\n    }\r\n}<\/pre>\n
    \u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a String<\/strong><\/div>\n

    \u5982\u679c\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5b83\u5c06\u88ab\u7528\u6765\u4ee3\u66ff\u4e0a\u9762\u663e\u793a\u7684\u7a7a\u683c\u5b57\u7b26\u3002<\/p>\n

    const user = {\r\n    name: \"DevPoint\",\r\n    age: 35,\r\n    address: {\r\n        city: \"Shenzhen\",\r\n    },\r\n};\r\n \r\nconsole.log(JSON.stringify(user, null, \"|---\"));<\/pre>\n

    \u8f93\u51fa\u6253\u5370\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

    {\r\n|---\"name\": \"DevPoint\",\r\n|---\"age\": 35,\r\n|---\"address\": {\r\n|---|---\"city\": \"Shenzhen\"\r\n|---}\r\n}<\/pre>\n
    toJSON \u65b9\u6cd5<\/strong><\/div>\n

    \u6709\u4e00\u4e2a\u540d\u4e3a toJSON \u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u662f\u4efb\u4f55\u5bf9\u8c61\u7684\u4e00\u90e8\u5206\u4f5c\u4e3a\u5176\u5c5e\u6027\u3002 JSON.stringify \u8fd4\u56de\u6b64\u51fd\u6570\u7684\u7ed3\u679c\u5e76\u5c06\u5176\u5b57\u7b26\u4e32\u5316\uff0c\u800c\u4e0d\u662f\u5c06\u6574\u4e2a\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n

    \/\/Initialize a User object\r\nconst user = {\r\n    name: \"DevPoint\",\r\n    city: \"Shenzhen\",\r\n    toJSON() {\r\n        return `\u59d3\u540d\uff1a${this.name}\uff0c\u6240\u5728\u57ce\u5e02\uff1a${this.city}`;\r\n    },\r\n};\r\n \r\nconsole.log(JSON.stringify(user)); \/\/ \"\u59d3\u540d\uff1aDevPoint\uff0c\u6240\u5728\u57ce\u5e02\uff1aShenzhen\"<\/pre>\n

    \u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eJS JSON.stringify()\u4f7f\u7528\u573a\u666f\u8be6\u89e3\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

    JSON.stringify() \u65b9\u6cd5\u5c06\u4e00\u4e2a JavaScript \u5bf9\u8c61\u6216\u503c\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff0c\u5982\u679c\u6307\u5b9a […]<\/p>\n","protected":false},"author":362,"featured_media":116344,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-262797","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\/262797","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\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=262797"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262797\/revisions"}],"predecessor-version":[{"id":262800,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262797\/revisions\/262800"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/116344"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=262797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=262797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=262797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}