{"id":305546,"date":"2024-05-20T19:48:59","date_gmt":"2024-05-20T11:48:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/305546.html"},"modified":"2024-05-20T19:49:05","modified_gmt":"2024-05-20T11:49:05","slug":"js-%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e5%b0%86%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e8%bd%ac%e4%b8%ba-json-%e6%a0%bc%e5%bc%8f%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/305546.html","title":{"rendered":"js \u7a0b\u5e8f\u5982\u4f55\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u4e3a json \u683c\u5f0f\u5b57\u7b26\u4e32"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26221829\/959439f2-1133-4b7b-898d-01e65c48b4a1.webp\" alt=\"js \u7a0b\u5e8f\u5982\u4f55\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u4e3a json \u683c\u5f0f\u5b57\u7b26\u4e32\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5b57\u7b26\u4e32\u4e3b\u8981\u6d89\u53ca\u4e24\u4e2a\u6b65\u9aa4\uff1a<strong>\u9996\u5148\u662f\u6784\u9020\u4e00\u4e2a\u80fd\u591f\u51c6\u786e\u63cf\u8ff0\u4e8c\u7ef4\u6570\u7ec4\u7ed3\u6784\u7684JSON\u5bf9\u8c61\uff0c\u5176\u6b21\u662f\u4f7f\u7528<code>JSON.stringify()<\/code>\u65b9\u6cd5\u5c06\u8be5\u5bf9\u8c61\u8f6c\u6362\u6210\u5b57\u7b26\u4e32<\/strong>\u3002\u901a\u5e38\uff0c\u8fd9\u4e2a\u8f6c\u6362\u8fc7\u7a0b\u53ef\u4ee5\u6839\u636e\u4e8c\u7ef4\u6570\u7ec4\u7684\u5185\u5bb9\u548c\u9884\u671f\u7684JSON\u7ed3\u6784\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u573a\u666f\uff0c\u5173\u952e\u5728\u4e8e\u5b9a\u4e49\u4e00\u4e2a\u6e05\u6670\u7684\u903b\u8f91\u6765\u8fed\u4ee3\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u5e76\u5c06\u5b83\u4eec\u6620\u5c04\u5230\u4e00\u4e2a\u5177\u6709\u7279\u5b9a\u683c\u5f0f\u7684JSON\u5bf9\u8c61\u4e0a\u3002<\/p>\n<\/p>\n<p><p><strong>\u9996\u5148\u63a2\u8ba8\u6784\u9020JSON\u5bf9\u8c61\u7684\u8fc7\u7a0b<\/strong>\uff1a\u7531\u4e8e\u4e8c\u7ef4\u6570\u7ec4\u672c\u8d28\u4e0a\u662f\u6570\u7ec4\u4e2d\u5d4c\u5957\u6570\u7ec4\uff0c\u8fd9\u4e2a\u7ed3\u6784\u81ea\u7136\u5730\u6620\u5c04\u5230JSON\u4e2d\u7684\u6570\u7ec4\u548c\u5bf9\u8c61\u3002\u5982\u679c\u4e8c\u7ef4\u6570\u7ec4\u7684\u5b50\u6570\u7ec4\u4ee3\u8868\u7279\u5b9a\u7684\u6570\u636e\u8bb0\u5f55\uff0c\u90a3\u4e48\u6bcf\u4e2a\u5b50\u6570\u7ec4\u53ef\u4ee5\u8f6c\u6362\u4e3aJSON\u5bf9\u8c61\u7684\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5176\u4e2d\u5b50\u6570\u7ec4\u7684\u5143\u7d20\u5bf9\u5e94\u4e8e\u5bf9\u8c61\u7684\u5c5e\u6027\u503c\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u9700\u8981\u9884\u5148\u5b9a\u4e49\u597dJSON\u5bf9\u8c61\u7684\u5c5e\u6027\u540d\uff0c\u4ee5\u4fbf\u4e8e\u6b63\u786e\u5730\u5c06\u6570\u7ec4\u7684\u5143\u7d20\u6620\u5c04\u5230JSON\u5bf9\u8c61\u7684\u5c5e\u6027\u4e0a\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6784\u9020JSON\u5bf9\u8c61<\/h3>\n<\/p>\n<p><p>\u4e8c\u7ef4\u6570\u7ec4\u901a\u5e38\u6709\u4e24\u79cd\u7c7b\u578b\u7684\u7ed3\u6784\uff0c\u6bcf\u79cd\u7ed3\u6784\u8f6c\u6362\u4e3aJSON\u65f6\u7684\u65b9\u6cd5\u7565\u6709\u4e0d\u540c\u3002\u7b2c\u4e00\u79cd\u662f\u6bcf\u4e2a\u5b50\u6570\u7ec4\u8868\u793a\u4e00\u4e2a\u6570\u636e\u8bb0\u5f55\uff0c\u5982\u8bb0\u5f55\u6709\u56fa\u5b9a\u7684\u5b57\u6bb5\uff1b\u7b2c\u4e8c\u79cd\u662f\u4e8c\u7ef4\u6570\u7ec4\u672c\u8eab\u8868\u793a\u4e00\u4e2a\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u6bd4\u5982\u77e9\u9635\u3002<\/p>\n<\/p>\n<p><p>\u9488\u5bf9\u7b2c\u4e00\u79cd\u60c5\u51b5\uff0c\u9996\u5148\u9700\u8981\u5b9a\u4e49\u4e00\u4e2aJSON\u5bf9\u8c61\u6a21\u677f\uff0c\u5176\u4e2d\u5305\u542b\u6240\u6709\u53ef\u80fd\u7684\u5c5e\u6027\u53ca\u5176\u5bf9\u5e94\u7684\u521d\u59cb\u503c\u3002\u7136\u540e\uff0c\u901a\u8fc7\u8fed\u4ee3\u4e8c\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e00\u4e2a\u5b50\u6570\u7ec4\uff0c\u6839\u636e\u8fd9\u4e9b\u5c5e\u6027\u540d\u548c\u5b50\u6570\u7ec4\u7684\u7d22\u5f15\uff0c\u5c06\u5b50\u6570\u7ec4\u7684\u503c\u6620\u5c04\u5230JSON\u5bf9\u8c61\u4e0a\u3002<\/p>\n<\/p>\n<p><p>\u5982\u679c\u4e8c\u7ef4\u6570\u7ec4\u4ee3\u8868\u4e00\u4e2a\u66f4\u590d\u6742\u7684\u7ed3\u6784\uff0c\u6bd4\u5982\u67d0\u79cd\u5f62\u5f0f\u7684\u77e9\u9635\uff0c\u8f6c\u6362\u8fc7\u7a0b\u53ef\u80fd\u5c31\u9700\u8981\u5d4c\u5957\u5faa\u73af\u6765\u904d\u5386\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u4e14\u6839\u636e\u5143\u7d20\u7684\u4f4d\u7f6e\u6784\u5efa\u5bf9\u5e94\u7684JSON\u7ed3\u6784\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>JSON.stringify()<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u6784\u5efa\u4e86\u4ee3\u8868\u539f\u59cb\u4e8c\u7ef4\u6570\u7ec4\u6570\u636e\u7684JSON\u5bf9\u8c61\uff0c\u4e0b\u4e00\u6b65\u5c31\u662f\u5c06\u8fd9\u4e2a\u5bf9\u8c61\u8f6c\u6362\u6210\u5b57\u7b26\u4e32\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7JavaScript\u5185\u7f6e\u7684<code>JSON.stringify()<\/code>\u51fd\u6570\u8f7b\u677e\u5b8c\u6210\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2aJavaScript\u503c\uff08\u5305\u62ec\u5bf9\u8c61\u548c\u6570\u7ec4\uff09\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u8fd9\u4e2a\u503c\u7684JSON\u683c\u5f0f\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p><code>JSON.stringify()<\/code>\u8fd8\u63a5\u53d7\u4e24\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u5206\u522b\u7528\u4e8e\u7f8e\u5316\u8f93\u51fa\u7684\u5b57\u7b26\u4e32\u548c\u8fc7\u6ee4\u5bf9\u8c61\u7684\u5c5e\u6027\u3002\u8fd9\u4e9b\u7279\u6027\u53ef\u4ee5\u5e2e\u52a9\u5728\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u4e2d\u5b9a\u5236\u5316\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u8003\u8651\u4e00\u4e2a\u8868\u793a\u5458\u5de5\u4fe1\u606f\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u884c\u4ee3\u8868\u4e00\u4e2a\u5458\u5de5\uff0c\u5217\u5206\u522b\u4ee3\u8868\u5458\u5de5\u7684\u59d3\u540d\u3001\u90e8\u95e8\u548c\u5de5\u8d44\u3002\u8f6c\u6362\u8fd9\u6837\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u5230JSON\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\uff0c\u9996\u5148\u9700\u8981\u8fed\u4ee3\u6bcf\u4e00\u884c\u6570\u7ec4\uff0c\u7136\u540e\u4e3a\u6bcf\u884c\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6700\u540e\u5c06\u8fd9\u4e9b\u5bf9\u8c61\u653e\u5165\u4e00\u4e2a\u6570\u7ec4\u4e2d\u3002\u8fd9\u6837\u5f62\u6210\u7684\u6570\u7ec4\u5373\u53ef\u901a\u8fc7<code>JSON.stringify()<\/code>\u65b9\u6cd5\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u4e0a\u8ff0\u903b\u8f91\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5f00\u53d1\u8005\u9700\u8981\u8003\u8651\u6570\u636e\u9a8c\u8bc1\u3001\u9519\u8bef\u5904\u7406\u7b49\u65b9\u9762\u7684\u95ee\u9898\uff0c\u4ee5\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u8f6c\u6362\u8fc7\u7a0b\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u9ad8\u7ea7\u6280\u5de7\u548c\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u4e3aJSON\u683c\u5f0f\u5b57\u7b26\u4e32\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u5229\u7528\u4e00\u4e9b\u9ad8\u7ea7\u6280\u5de7\uff0c\u5982\u4f7f\u7528<code>map()<\/code>\u51fd\u6570\u7b80\u5316\u6570\u7ec4\u7684\u8fed\u4ee3\u548c\u8f6c\u6362\u8fc7\u7a0b\uff0c\u6216\u8005\u4f7f\u7528<code>reduce()<\/code>\u51fd\u6570\u5c06\u6570\u7ec4\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u7684\u4e2d\u95f4\u7ed3\u679c\u7d2f\u79ef\u8d77\u6765\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u9700\u8981\u6ce8\u610f\u63a7\u5236\u6700\u7ec8JSON\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u4ee5\u53ca\u5904\u7406\u90a3\u4e9b\u53ef\u80fd\u65e0\u6cd5\u76f4\u63a5\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u6570\u636e\u7c7b\u578b\uff0c\u5982\u65e5\u671f\u5bf9\u8c61\u6216\u5305\u542b\u5faa\u73af\u5f15\u7528\u7684\u7ed3\u6784\u3002\u4f7f\u7528\u5408\u9002\u7684\u6570\u636e\u5904\u7406\u548c\u9a8c\u8bc1\u65b9\u6cd5\uff0c\u53ef\u4ee5\u786e\u4fdd\u6700\u7ec8\u751f\u6210\u7684JSON\u5b57\u7b26\u4e32\u65e2\u7cbe\u786e\u53c8\u5177\u6709\u8f83\u597d\u7684\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1.\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\n\u901a\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 JavaScript \u4e2d\u7684 <code>JSON.stringify()<\/code> \u65b9\u6cd5\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u6709\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4 <code>arr<\/code> \uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5176\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\uff1a<\/p>\n<pre><code class=\"language-javascript\">var arr = [[1, 2], [3, 4], [5, 6]];\nvar jsonString = JSON.stringify(arr);\nconsole.log(jsonString);\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>[[1,2],[3,4],[5,6]]<\/code>\u3002<\/p>\n<p><strong>2.\u5982\u4f55\u5904\u7406\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u7a7a\u503c\uff0c\u5728\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u5b57\u7b26\u4e32\u65f6\u4fdd\u7559\u5b83\u4eec\u7684\u4f4d\u7f6e\uff1f<\/strong><br \/>\n\u5982\u679c\u4f60\u60f3\u5728\u8f6c\u6362\u8fc7\u7a0b\u4e2d\u4fdd\u7559\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u7a7a\u503c\uff0c\u5e76\u4e14\u4fdd\u6301\u5176\u5728 JSON \u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\u4e0d\u53d8\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684 replacer \u51fd\u6570\u4f5c\u4e3a <code>JSON.stringify()<\/code> \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">var arr = [[1, 2], [null, 4], [5, null]];\nvar jsonString = JSON.stringify(arr, function(key, value) {\n  if (value === null) {\n    return &#039;null&#039;;\n  }\n  return value;\n});\nconsole.log(jsonString);\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>[[1,2],[null,4],[5,null]]<\/code>\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u7a7a\u503c\u66ff\u6362\u4e3a\u5b57\u7b26\u4e32 <code>&#039;null&#039;<\/code> \u6765\u4fdd\u7559\u5176\u4f4d\u7f6e\u3002<\/p>\n<p><strong>3.\u5982\u679c\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u5305\u542b\u590d\u6742\u5bf9\u8c61\uff0c\u5982\u4f55\u5c06\u5176\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u5b57\u7b26\u4e32\uff1f<\/strong><br \/>\n\u5982\u679c\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u662f\u590d\u6742\u7684\u5bf9\u8c61\uff08\u4f8b\u5982\uff0c\u5305\u542b\u51fd\u6570\u3001\u65e5\u671f\u7b49\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>JSON.stringify()<\/code> \u65b9\u6cd5\u7684\u7b2c\u4e09\u4e2a\u53c2\u6570\u6765\u5904\u7406\u8fd9\u4e9b\u5bf9\u8c61\u7684\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">var arr = [[{name: &#039;John&#039;, age: 25}, {name: &#039;Jane&#039;, age: 30}], [{name: &#039;Bob&#039;, age: 35}, {name: &#039;Alice&#039;, age: 40}]];\nvar jsonString = JSON.stringify(arr, function(key, value) {\n  if (typeof value === &#039;function&#039;) {\n    return value.toString();\n  }\n  if (value instanceof Date) {\n    return value.toISOString();\n  }\n  return value;\n});\nconsole.log(jsonString);\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u8f93\u51fa\uff1a<code>[[{&quot;name&quot;:&quot;John&quot;,&quot;age&quot;:25},{&quot;name&quot;:&quot;Jane&quot;,&quot;age&quot;:30}],[{&quot;name&quot;:&quot;Bob&quot;,&quot;age&quot;:35},{&quot;name&quot;:&quot;Alice&quot;,&quot;age&quot;:40}]]<\/code>\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684 replacer \u51fd\u6570\u5c06\u51fd\u6570\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u65e5\u671f\u5bf9\u8c61\u8f6c\u6362\u4e3a ISO 8601 \u683c\u5f0f\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\uff0c\u5c06\u4e8c\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5b57\u7b26\u4e32\u4e3b\u8981\u6d89\u53ca\u4e24\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\u662f\u6784\u9020\u4e00\u4e2a\u80fd\u591f\u51c6\u786e\u63cf\u8ff0\u4e8c\u7ef4\u6570 [&hellip;]","protected":false},"author":3,"featured_media":305551,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/305546"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=305546"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/305546\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/305551"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=305546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=305546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=305546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}