{"id":2749,"date":"2024-11-10T14:25:19","date_gmt":"2024-11-10T06:25:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/2749\/"},"modified":"2024-11-10T14:25:19","modified_gmt":"2024-11-10T06:25:19","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%b8%a6%e7%b4%a2%e5%bc%95%e7%9a%84%e6%89%81%e5%b9%b3-json-%e6%95%b0%e6%8d%ae%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%b5%8c%e5%a5%97-json-%e5%af%b9%e8%b1%a1%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/2749\/","title":{"rendered":"\u5982\u4f55\u5c06\u5e26\u7d22\u5f15\u7684\u6241\u5e73 JSON \u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957 JSON \u5bf9\u8c61\u5217\u8868\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173087100314771.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5c06\u5e26\u7d22\u5f15\u7684\u6241\u5e73 JSON \u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957 JSON \u5bf9\u8c61\u5217\u8868\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5c06\u5e26\u7d22\u5f15\u7684\u6241\u5e73 JSON \u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957 JSON \u5bf9\u8c61\u5217\u8868\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5c06 json \u8f6c\u6362\u4e3a\u5d4c\u5957 json \u5bf9\u8c61\u7684\u5217\u8868<\/strong><\/p>\n<p>\u60a8\u63d0\u5230\u7684 json \u5177\u6709\u4ee5\u4e0b\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>\u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5d4c\u5957 json \u5bf9\u8c61\u7684\u5217\u8868\uff0c\u60a8\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u5c06 json \u89e3\u6790\u4e3a key-value \u5bf9\uff1a<\/li>\n<\/ol>\n<pre>const keyvaluepairs = object.entries(json);<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u6765\u5b58\u50a8\u8f6c\u6362\u540e\u7684\u5bf9\u8c61\uff1a<\/li>\n<\/ol>\n<pre>const result = [];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u904d\u5386 key-value \u5bf9\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u952e\u521b\u5efa\u5bf9\u5e94\u7684 json \u5bf9\u8c61\uff1a<\/li>\n<\/ol>\n<pre>for (const [key, value] of keyvaluepairs) {\n  const [, index] = key.match(\/family[(d+)]\/);\n  result[index] = result[index] || { familyname: '', sex: '' };\n  result[index][key.split('.').pop()] = value;\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u8fd4\u56de\u8f6c\u6362\u540e\u7684\u5217\u8868\uff1a<\/li>\n<\/ol>\n<pre>return result;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6700\u7ec8\u5f97\u5230\u7684\u8f6c\u6362\u540e\u7684 json \u5217\u8868\u5c06\u5982\u4e0b\u6240\u793a\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>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5c06\u5e26\u7d22\u5f15\u7684\u6241\u5e73 JSON \u6570\u636e\u8f6c\u6362\u4e3a\u5d4c\u5957 JSON \u5bf9\u8c61\u5217\u8868\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>\u5982\u4f55\u5c06 json \u8f6c\u6362\u4e3a\u5d4c\u5957 json \u5bf9\u8c61\u7684\u5217\u8868 \u60a8\u63d0\u5230\u7684 json \u5177\u6709\u4ee5\u4e0b\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 \u8981\u5c06\u5176\u8f6c\u6362\u4e3a\u5d4c\u5957 json \u5bf9\u8c61\u7684\u5217\u8868\uff0c\u60a8\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u5c06 json \u89e3\u6790\u4e3a key-value \u5bf9\uff1a const keyvaluepairs = object.entries(json); \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\u6765\u5b58\u50a8\u8f6c\u6362\u540e\u7684\u5bf9\u8c61\uff1a const result = []; \u767b\u5f55\u540e\u590d\u5236 \u904d\u5386 key-value \u5bf9\uff0c\u5e76\u4e3a\u6bcf\u4e2a\u952e\u521b\u5efa\u5bf9\u5e94\u7684 json \u5bf9\u8c61\uff1a for (const [key, value] of keyvaluepairs) { const [, index] = key.match(\/family[(d+)]\/); [&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-2749","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2749","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=2749"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/2749\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=2749"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=2749"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=2749"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}