{"id":2654,"date":"2024-11-10T09:09:32","date_gmt":"2024-11-10T01:09:32","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2654\/"},"modified":"2024-11-10T09:09:32","modified_gmt":"2024-11-10T01:09:32","slug":"%e5%a6%82%e4%bd%95%e5%b0%86-json-%e6%a0%bc%e5%bc%8f%e6%95%b0%e6%8d%ae%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%88%97%e8%a1%a8%e6%a0%bc%e5%bc%8f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2654\/","title":{"rendered":"\u5982\u4f55\u5c06 JSON \u683c\u5f0f\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173085355282009.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06 JSON \u683c\u5f0f\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06 JSON \u683c\u5f0f\u6570\u636e\u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u4f7f\u7528 javascript \u8f6c\u6362 json \u683c\u5f0f<\/strong><\/p>\n<p>\u95ee\u9898\uff1a\u5982\u4f55\u5c06\u4ee5\u4e0b json \u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\uff1f<\/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\u4e3a\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 <\/p>\n<p>\u56de\u7b54\uff1a<\/p>\n<p>\u4e3a\u4e86\u5c06 json \u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a<\/p>\n<ol>\n<li>\n<p>\u4f7f\u7528 object.entries() \u65b9\u6cd5\u5c06 json \u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b\u6570\u7ec4\u3002<\/p>\n<pre>const entries = object.entries(json);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u952e\u503c\u5bf9\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u521b\u5efa\u76f8\u5e94\u7684\u5206\u79bb\u952e\u3002<\/p>\n<pre>const keys = entries.map(([key, value]) =&gt; key.replace(\/family[(d+)].\/, \"\"));<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u4f7f\u7528 reduce() \u65b9\u6cd5\u521b\u5efa\u5217\u8868\uff0c\u5176\u4e2d\u5305\u542b\u7ed9\u5b9a\u952e\u7684\u65b0\u5bf9\u8c61\u3002<\/p>\n<pre>const list = keys.reduce((acc, key) =&gt; {\n  const items = entries.filter(([currentkey]) =&gt; currentkey.startswith(key));\n  acc.push({ [key]: items[0][1], sex: items[1][1] });\n  return acc;\n}, []);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p>\u8fd4\u56de\u521b\u5efa\u7684\u5217\u8868\u3002<\/p>\n<pre>return list;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06 JSON \u683c\u5f0f\u6570\u636e\u8f6c\u6362\u4e3a\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 \u95ee\u9898\uff1a\u5982\u4f55\u5c06\u4ee5\u4e0b json \u683c\u5f0f\u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\uff1f { &#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\u4e3a\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 \u56de\u7b54\uff1a \u4e3a\u4e86\u5c06 json \u8f6c\u6362\u4e3a\u5217\u8868\u683c\u5f0f\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\uff1a \u4f7f\u7528 object.entries() \u65b9\u6cd5\u5c06 json \u5bf9\u8c61\u8f6c\u6362\u4e3a\u4e00\u4e2a\u5305\u542b\u6570\u7ec4\u3002 const entries = object.entries(json); \u767b\u5f55\u540e\u590d\u5236 \u5bf9\u4e8e\u6bcf\u4e2a\u952e\u503c\u5bf9\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u521b\u5efa\u76f8\u5e94\u7684\u5206\u79bb\u952e\u3002 const keys = entries.map(([key, value]) [&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-2654","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2654","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=2654"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2654\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2654"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}