{"id":2721,"date":"2024-11-10T12:07:48","date_gmt":"2024-11-10T04:07:48","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2721\/"},"modified":"2024-11-10T12:07:48","modified_gmt":"2024-11-10T04:07:48","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-javascript-%e5%b0%86%e5%b5%8c%e5%a5%97%e7%9a%84-json-%e6%a0%bc%e5%bc%8f%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%a0%87%e5%87%86%e7%9a%84%e5%88%97%e8%a1%a8%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2721\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u5d4c\u5957\u7684 JSON \u683c\u5f0f\u8f6c\u6362\u4e3a\u6807\u51c6\u7684\u5217\u8868\u683c\u5f0f\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173086399297772.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u5d4c\u5957\u7684 JSON \u683c\u5f0f\u8f6c\u6362\u4e3a\u6807\u51c6\u7684\u5217\u8868\u683c\u5f0f\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u5d4c\u5957\u7684 JSON \u683c\u5f0f\u8f6c\u6362\u4e3a\u6807\u51c6\u7684\u5217\u8868\u683c\u5f0f\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 javascript \u8f6c\u6362 json \u683c\u5f0f<\/strong><\/p>\n<p>\u60f3\u8981\u5c06\u63d0\u4f9b\u7ed9\u7684 json \u683c\u5f0f\uff1a<\/p>\n<pre>{\n  \"family[0].familyname\": \"\u7b2c\u4e00\u4e2a\",\n  \"family[0].sex\": \"1\",\n  \"family[1].familyname\": \"\u7b2c\u4e8c\u4e2a\",\n  \"family[1].sex\": \"1\",\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8f6c\u6362\u6210\u4ee5\u4e0b\u671f\u671b\u683c\u5f0f\uff1a<\/p>\n<pre>[\n  {\n    \"familyname\": \"\u7b2c\u4e00\u4e2a\",\n    \"sex\": \"1\"\n  },\n  {\n    \"familyname\": \"\u7b2c\u4e8c\u4e2a\",\n    \"sex\": \"1\"\n  }\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u8f6c\u6362\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li><strong>\u89e3\u6790 json \u6210\u5bf9\u8c61\uff1a<\/strong><\/li>\n<\/ol>\n<pre>const json = {\n  \"family[0].familyname\": \"\u7b2c\u4e00\u4e2a\",\n  \"family[0].sex\": \"1\",\n  \"family[1].familyname\": \"\u7b2c\u4e8c\u4e2a\",\n  \"family[1].sex\": \"1\",\n};\n\nconst keyvaluepairs = object.entries(json);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u6839\u636e\u952e\u503c\u5bf9\u5bf9\u8c61\u751f\u6210\u5217\u8868\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u952e\u503c\u5bf9\u5bf9\u8c61\u4e2d\u7684\u952e\u6765\u521b\u5efa person \u7c7b\u7684\u5b9e\u4f8b\uff0c\u5e76\u5c06\u6bcf\u4e2a\u5b9e\u4f8b\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<pre>const personlist = [];\nfor (const [key, value] of keyvaluepairs) {\n  const [index, property] = key.split(\".\");\n  let person = personlist[index];\n\n  if (!person) {\n    person = {};\n    personlist.push(person);\n  }\n\n  person[property] = value;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li><strong>\u8fd4\u56de\u8f6c\u6362\u540e\u7684\u5217\u8868\uff1a<\/strong><\/li>\n<\/ol>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u8fd4\u56de\u8f6c\u6362\u540e\u7684\u5217\u8868\u5373\u53ef\u3002<\/p>\n<pre>console.log(personlist);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<pre>[\n  {\n    \"familyname\": \"\u7b2c\u4e00\u4e2a\",\n    \"sex\": \"1\"\n  },\n  {\n    \"familyname\": \"\u7b2c\u4e8c\u4e2a\",\n    \"sex\": \"1\"\n  }\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528 JavaScript \u5c06\u5d4c\u5957\u7684 JSON \u683c\u5f0f\u8f6c\u6362\u4e3a\u6807\u51c6\u7684\u5217\u8868\u683c\u5f0f\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 javascript \u8f6c\u6362 json \u683c\u5f0f \u60f3\u8981\u5c06\u63d0\u4f9b\u7ed9\u7684 json \u683c\u5f0f\uff1a { &#8220;family[0].familyname&#8221;: &#8220;\u7b2c\u4e00\u4e2a&#8221;, &#8220;family[0].sex&#8221;: &#8220;1&#8221;, &#8220;family[1].familyname&#8221;: &#8220;\u7b2c\u4e8c\u4e2a&#8221;, &#8220;family[1].sex&#8221;: &#8220;1&#8221;, } \u767b\u5f55\u540e\u590d\u5236 \u8f6c\u6362\u6210\u4ee5\u4e0b\u671f\u671b\u683c\u5f0f\uff1a [ { &#8220;familyname&#8221;: &#8220;\u7b2c\u4e00\u4e2a&#8221;, &#8220;sex&#8221;: &#8220;1&#8221; }, { &#8220;familyname&#8221;: &#8220;\u7b2c\u4e8c\u4e2a&#8221;, &#8220;sex&#8221;: &#8220;1&#8221; } ] \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u8f6c\u6362\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u89e3\u6790 json \u6210\u5bf9\u8c61\uff1a const json = { &#8220;family[0].familyname&#8221;: &#8220;\u7b2c\u4e00\u4e2a&#8221;, &#8220;family[0].sex&#8221;: &#8220;1&#8221;, &#8220;family[1].familyname&#8221;: &#8220;\u7b2c\u4e8c\u4e2a&#8221;, &#8220;family[1].sex&#8221;: &#8220;1&#8221;, }; const [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-2721","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2721","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=2721"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2721\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2721"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2721"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2721"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}