{"id":208048,"date":"2025-07-08T16:49:20","date_gmt":"2025-07-08T08:49:20","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208048\/"},"modified":"2025-07-08T16:49:20","modified_gmt":"2025-07-08T08:49:20","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-json-decoder-%e7%9c%81%e7%95%a5%e7%a9%ba-json-%e5%ad%97%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208048\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528 json.decoder \u7701\u7565\u7a7a json \u5b57\u6bb5"},"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>\u5982\u4f55\u4f7f\u7528 json.decoder \u7701\u7565\u7a7a json \u5b57\u6bb5<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-28 11:18:25<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u73cd\u60dc\u65f6\u95f4\uff0c\u52e4\u594b\u5b66\u4e60\uff01\u4eca\u5929\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u4f7f\u7528 json.decoder \u7701\u7565\u7a7a json \u5b57\u6bb5\u300b<\/span>\uff0c\u6b63\u6587\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u6216\u8005\u662f\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u6709\u7591\u95ee\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u6211\uff01\u540e\u9762\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u5185\u5bb9\u7684\uff0c\u5e0c\u671b\u90fd\u80fd\u5e2e\u5230\u6b63\u5728\u5b66\u4e60\u7684\u5927\u5bb6\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u5c1d\u8bd5\u7406\u89e3\u4e3a\u4ec0\u4e48\u4e24\u4e2a\u51fd\u6570\u8fd4\u56de\u76f8\u540c\u7684\u8f93\u51fa\u3002 \u636e\u6211\u4e86\u89e3\uff0c\u7701\u7565\u7a7a\u7684\u76ee\u7684\u662f\u4e0d\u5c06\u8be5\u952e\u6dfb\u52a0\u5230\u7ed3\u679c\u7ed3\u6784\u4e2d\u3002<\/p>\n<p>\u6211\u5199\u4e86\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6211\u671f\u671b\u7b2c\u4e00\u4e2a\u8f93\u51fa\u6ca1\u6709\u201cempty\u201d\u952e\uff0c\u4f46\u7531\u4e8e\u67d0\u79cd\u539f\u56e0\u5b83\u7684\u503c\u4ecd\u7136\u663e\u793a\u4e3a 0\u3002<\/p>\n<pre>package main\n\nimport (\n    \"encoding\/json\"\n    \"fmt\"\n    \"strings\"\n)\n\ntype agentOmitEmpty struct {\n    Alias   string `json:\"Alias,omitempty\"`\n    Skilled bool   `json:\"Skilled,omitempty\"`\n    FinID   int32  `json:\"FinId,omitempty\"`\n    Empty   int    `json:\"Empty,omitempty\"`\n}\n\ntype agent struct {\n    Alias   string `json:\"Alias\"`\n    Skilled bool   `json:\"Skilled\"`\n    FinID   int32  `json:\"FinId\"`\n    Empty   int    `json:\"Empty\"`\n}\n\nfunc main() {\n    jsonString := `{\n        \"Alias\":\"Robert\",\n        \"Skilled\":true,\n        \"FinId\":12345\n    }`\n\n    fmt.Printf(\"output with omit emtpy: %v\\n\", withEmpty(strings.NewReader(jsonString)))\n    \/\/ output with omit emtpy: {Robert true 12345 0}\n\n    fmt.Printf(\"output regular: %v\\n\", withoutEmpty(strings.NewReader(jsonString)))\n    \/\/ output without omit: {Robert true 12345 0}\n}\n\nfunc withEmpty(r *strings.Reader) agentOmitEmpty {\n    dec := json.NewDecoder(r)\n    body := agentOmitEmpty{}\n    err := dec.Decode(&amp;body)\n    if err != nil {\n        panic(err)\n    }\n    return body\n}\n\nfunc withoutEmpty(r *strings.Reader) agent {\n    dec := json.NewDecoder(r)\n    body := agent{}\n    err := dec.Decode(&amp;body)\n    if err != nil {\n        panic(err)\n    }\n    return body\n}\n<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u9700\u8981\u5c06 Empty \u5b9a\u4e49\u4e3a <code>*int<\/code>\uff0c\u8fd9\u6837\u5f53\u6ca1\u6709\u503c\u65f6\u5b83\u5c06\u88ab\u66ff\u6362\u4e3a nil\u3002\u90a3\u4e48\u5c31\u4e0d\u4f1a\u4fdd\u5b58\u5728\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u4f7f\u7528 json.decoder \u7701\u7565\u7a7a json \u5b57\u6bb5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\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-208048","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208048","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=208048"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208048\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}