{"id":207167,"date":"2025-07-08T08:54:40","date_gmt":"2025-07-08T00:54:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207167\/"},"modified":"2025-07-08T08:54:40","modified_gmt":"2025-07-08T00:54:40","slug":"%e8%a7%a3%e6%9e%90%e5%b5%8c%e5%a5%97%e7%9a%84-json-%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207167\/","title":{"rendered":"\u89e3\u6790\u5d4c\u5957\u7684 json \u6587\u4ef6"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u89e3\u6790\u5d4c\u5957\u7684 json \u6587\u4ef6<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 15:15:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4e00\u5206\u8015\u8018\uff0c\u4e00\u5206\u6536\u83b7\uff01\u65e2\u7136\u90fd\u6253\u5f00\u8fd9\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u89e3\u6790\u5d4c\u5957\u7684 json \u6587\u4ef6\u300b<\/span>\uff0c\u5c31\u575a\u6301\u770b\u4e0b\u53bb\uff0c\u5b66\u4e0b\u53bb\u5427\uff01\u672c\u6587\u4e3b\u8981\u4f1a\u7ed9\u5927\u5bb6\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5927\u5bb6\u5bf9\u672c\u6587\u6709\u597d\u7684\u5efa\u8bae\u6216\u8005\u770b\u5230\u6709\u4e0d\u8db3\u4e4b\u5904\uff0c\u975e\u5e38\u6b22\u8fce\u5927\u5bb6\u79ef\u6781\u63d0\u51fa\uff01\u5728\u540e\u7eed\u6587\u7ae0\u6211\u4f1a\u7ee7\u7eed\u66f4\u65b0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u90fd\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u89e3\u6790\u4e00\u4e2a\u5177\u6709\u4e0d\u5bfb\u5e38\u7ed3\u6784\u7684 json \u6587\u4ef6\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>{\n    \"394885\": \n    {\n        \"record\": \n        {\n            \"student_name\": \"daryl jones\",\n            \"student_number\": 123884,\n            \"student_dob\": \"12\/10\/1982\",\n            \"student_email\": \"[email&nbsp;protected]\",\n        }    \n    },\n}<\/pre>\n<p>\u6211\u4e00\u76f4\u5728\u7814\u7a76\u4e00\u4e9b\u4ee3\u7801\u6f14\u793a\uff0c\u4f46\u6211\u60f3\u5c06\u5b83\u4eec\u5168\u90e8\u653e\u5165\u4e00\u4e2a\u7ed3\u6784\u4e2d\uff0c\u7136\u540e\u6211\u8ba1\u5212\u901a\u8fc7\u6570\u5b57\u5b57\u7b26\u4e32\uff08\u6211\u731c\u7684\u5bf9\u8c61\u540d\u79f0\uff09\u8fdb\u884c\u641c\u7d22\uff1f<\/p>\n<p>\u6211\u5bf9 json \u6216 go \u4e0d\u592a\u64c5\u957f\uff0c\u8fd9\u662f\u6211\u5230\u76ee\u524d\u4e3a\u6b62\u7f16\u5199\u7684\u4ee3\u7801\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"io\/ioutil\"\n    \"os\"\n)\n\ntype id struct {\n    recordid string\n    record   []record\n}\n\ntype record struct {\n    name   string\n    number uint32\n    dob    string\n    email  string\n}\n\nfunc main() {\n    jsonFile, err := os.Open(\"\/home\/emyrw\/development\/go\/src\/json_processor\/demo.json\")\n    if err != nil {\n        fmt.Println(err)\n    } else {\n        var records id\n\n        byteValue, _ := ioutil.ReadAll(jsonFile)\n        json.Unmarshal(byteValue, &amp;records)\n        fmt.Println(records)\n\n        fmt.Println(\"opened demo.json\")\n        defer jsonFile.Close()\n    }\n}<\/pre>\n<p>\u6211\u4e0d\u786e\u5b9a\u6211\u662f\u5426\u6b63\u786e\uff0c\u4f46\u5f88\u91cd\u89c6\u4efb\u4f55\u4eba\u63d0\u4f9b\u7684\u4efb\u4f55\u63d0\u793a\u6216\u5efa\u8bae\u3002\u6211\u4e00\u76f4\u5728\u8c37\u6b4c\u641c\u7d22\uff0c\u4f46\u6211\u53d1\u73b0\u6ca1\u6709\u4e00\u4e2a\u6837\u672c\u975e\u5e38\u9002\u5408\u6211\u7684\u573a\u666f\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u5c06\u6b64\u7ed3\u6784\u7528\u4e8e\u60a8\u7684 id \u7c7b\u578b<\/p>\n<pre>type id struct {\n    record map[string]record\n}<\/pre>\n<p><strong>\u7f16\u8f91<\/strong><\/p>\n<p>\u8fd9\u662f\u5e26\u6709\u4e00\u4e9b\u89e3\u91ca\u7684\u5de5\u4f5c\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p>\u7531\u4e8e\u60a8\u6709\u4e00\u4e2a\u591a\u7ea7 json\uff0c\u56e0\u6b64\u60a8\u53ef\u4ee5\u5c06\u5176\u89e3\u6790\u4e3a\u5d4c\u5957\u7ed3\u6784\u3002<\/p>\n<pre>{\n        \"student_name\": \"daryl jones\",\n        \"student_number\": 123884,\n        \"student_dob\": \"12\/10\/1982\",\n        \"student_email\": \"\"\n}<\/pre>\n<p>\u8981\u89e3\u6790 json \u7684\u8fd9\u4e00\u90e8\u5206\uff0c\u60a8\u9700\u8981\u8fd9\u4e2a\u7ed3\u6784<\/p>\n<pre>type record struct {\n    name   string `json:\"student_name\"`\n    number uint32 `json:\"student_number\"`\n    dob    string `json:\"student_dob\"`\n    email  string `json:\"student_email\"`\n}<\/pre>\n<p>\u5b57\u6bb5\u5fc5\u987b\u5bfc\u51fa\uff08\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff09\u5e76\u5177\u6709\u4e0e json \u5c5e\u6027\u5339\u914d\u7684 json \u6807\u7b7e\u3002<\/p>\n<pre>{\n    \"record\": \n    {\n        \"student_name\": \"daryl jones\",\n        \"student_number\": 123884,\n        \"student_dob\": \"12\/10\/1982\",\n        \"student_email\": \"\"\n    }    \n}<\/pre>\n<p>\u8981\u4f7f\u8fd9\u90e8\u5206\u5de5\u4f5c\uff0c\u60a8\u9700\u8981\u50cf\u8fd9\u6837\u7684\u5d4c\u5957\u7ed3\u6784<\/p>\n<pre>type id struct {\n     record record\n}<\/pre>\n<p>\u5f52\u6863\u540d\u79f0\u4f1a\u518d\u6b21\u5bfc\u51fa\uff0c\u4f46\u7531\u4e8e\u5b83\u4e0e\u60a8\u7684 json \u5c5e\u6027\u5339\u914d\uff0c\u56e0\u6b64\u60a8\u4e0d\u9700\u8981\u8be5\u6807\u8bb0\u3002 <\/p>\n<pre>{\n\"394885\": \n  {\n    \"record\": \n    {\n        \"student_name\": \"daryl jones\",\n        \"student_number\": 123884,\n        \"student_dob\": \"12\/10\/1982\",\n        \"student_email\": \"\"\n    }    \n  }\n}<\/pre>\n<p>\u7531\u4e8e\u9876\u7ea7\u5c5e\u6027\u540d\u79f0\u662f\u5b66\u751f id\uff0c\u56e0\u6b64\u60a8\u53ef\u4ee5\u4f7f\u7528\u6620\u5c04\u800c\u4e0d\u662f\u7ed3\u6784<\/p>\n<pre>var records map[string]id<\/pre>\n<p>\u5e76\u786e\u4fdd\u60a8\u6ca1\u6709\u5c3e\u968f\u9017\u53f7\uff0c\u56e0\u4e3a json \u89c4\u8303\u4e0d\u5141\u8bb8\u8fd9\u6837\u505a<\/p>\n<p>\u9996\u5148\uff0c\u60a8\u7684 json \u65e0\u6548\u3002 \u4e0e golang \u7ed3\u6784\u4e0d\u540c\uff0c\u60a8\u4e0d\u9700\u8981\u5c06 <code>\u3001<\/code> \u653e\u5728\u6700\u7ec8\u5bf9\u8c61\u540e\u9762\u3002<\/p>\n<p>{ \u201c394885\u201d\uff1a { \u201c\u8bb0\u5f55\u201d\uff1a { &#8220;student_name&#8221;: &#8220;\u8fbe\u91cc\u5c14\u00b7\u743c\u65af&#8221;, \u201c\u5b66\u53f7\u201d\uff1a123884\uff0c &#8220;student_dob&#8221;: &#8220;1982\/12\/10&#8221;, \u201c\u5b66\u751f\u90ae\u7bb1\u201d\uff1a\u201c\u201d }<br \/> } }<\/p>\n<p>\u7b2c\u4e8c\uff0c <code>var \u8bb0\u5f55 <\/code> \u5e94\u8be5\u51c6\u786e\u5730\u5efa\u6a21\u60a8\u7684 json\uff0c\u65b9\u6cd5\u662f\u901a\u8fc7\u4e0e json \u5bf9\u8c61\u4e2d\u7684\u540d\u79f0\u5b8c\u5168\u4e00\u81f4\u7684\u65b9\u5f0f\u547d\u540d\u7ed3\u6784\u4e2d\u7684\u5b57\u6bb5\uff0c\u6216\u8005\u4f7f\u7528\u5c5e\u6027\u3002<\/p>\n<p>json \u7684\u5916\u90e8\u4e0d\u662f <code>id<\/code> \u7c7b\u578b\uff0c\u800c\u662f <code>map[string]id<\/code> \u7c7b\u578b<\/p>\n<p><code>id<\/code> \u6ca1\u6709 <code>recordid<\/code> \u5b57\u6bb5\uff0c\u4f46\u662f\u5b83\u6709 <code>record<\/code> \u5b57\u6bb5\uff0c\u4f46\u662f\uff0c\u5982\u679c\u8981\u5c06 json \u5e8f\u5217\u5316\u5230\u7ed3\u6784\u4e2d\uff0c\u5219\u5fc5\u987b\u5bfc\u51fa\u7ed3\u6784\u7684\u5b57\u6bb5\uff08\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff09\u3002<\/p>\n<p>\u8fd9\u5c31\u662f\u5c5e\u6027\u6d3e\u4e0a\u7528\u573a\u7684\u5730\u65b9\uff0c<\/p>\n<pre>type id struct {\n    record []record `json:\"record\"`\n}\n<\/pre>\n<p>\u4e0e <code>record<\/code> \u7ed3\u6784\u76f8\u540c\uff0c<\/p>\n<pre>type record struct {\n    Name string `json:\"student_name\"`\n    Email string `json:\"student_email\"`\n    \/\/ You get the idea...\n}\n<\/pre>\n<p>\u7b2c\u4e09\uff0c \u6253\u5f00\u6587\u4ef6\u540e\u7acb\u5373\u6dfb\u52a0 <code>defer<\/code> \u8bed\u53e5\uff0c \u628a\u5b83\u653e\u5728\u5757\u7684\u672b\u5c3e\u5c31\u8fbe\u4e0d\u5230\u76ee\u7684\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u89e3\u6790\u5d4c\u5957\u7684 json \u6587\u4ef6\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207167","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207167"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207167\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}