{"id":207673,"date":"2025-07-08T10:32:11","date_gmt":"2025-07-08T02:32:11","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207673\/"},"modified":"2025-07-08T10:32:11","modified_gmt":"2025-07-08T02:32:11","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%b5%8c%e5%a5%97%e7%bb%93%e6%9e%84%e7%bc%96%e7%bb%84%e4%b8%ba%e5%b9%b3%e9%9d%a2-json","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207673\/","title":{"rendered":"\u5982\u4f55\u5c06\u5d4c\u5957\u7ed3\u6784\u7f16\u7ec4\u4e3a\u5e73\u9762 JSON"},"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\u5c06\u5d4c\u5957\u7ed3\u6784\u7f16\u7ec4\u4e3a\u5e73\u9762 JSON<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 15:21:33<\/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\u5982\u4f55\u5c06\u5d4c\u5957\u7ed3\u6784\u7f16\u7ec4\u4e3a\u5e73\u9762 JSON\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\u5c1d\u8bd5\u5c06\u5177\u6709\u591a\u4e2a\u76f8\u540c\u7ed3\u6784\u7684\u5d4c\u5957\u7ed3\u6784\u7f16\u7ec4\u4e3a\u5e73\u9762 json \u7ed3\u6784 e.g.<\/p>\n<pre>type a struct {\n    value float64\n    unit  string\n}\n<\/pre>\n<pre>type b struct {\n    p1 string `json:p1`\n    p2 int    `json:p1`\n    ...\n    a1 a      `json:\"a1,omitempty\"`\n    a2 a      `json:\"a1,omitempty\"`\n    ...\n}<\/pre>\n<p>\u8c03\u7528 <code>json.marshall(b)<\/code> \u65f6\uff0c\u76ee\u6807\u662f\u83b7\u53d6\u4f7f\u7528\u4e0a\u8ff0\u4ee3\u7801\u6253\u5370\u5e73\u9762\u7ed3\u6784\u7684 json \u7ed3\u6784\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u4ee3\u66ff<\/p>\n<pre>{\n    \"p1\": \"...\",\n    \"p2\": 1,\n    ...\n    \"a1\": {...}\n    \"a2\": {...}\n    ...\n}<\/pre>\n<p>\u6211\u4f1a\u5f97\u5230\u7c7b\u4f3c\u4e8e\u7684\u7ed3\u6784<\/p>\n<pre>{\n    \"p1\": \"...\",\n    \"p2\": 1,\n    ...\n    \"a1\": 1.1,\n    \"a1_u\": \"unit\",\n    \"a2\": 1.2,\n    \"a2_u\": \"unit\",\n    ...\n}<\/pre>\n<p>\u6211\u7814\u7a76\u8fc7\u5d4c\u5165\u7684\u6982\u5ff5\uff0c\u4f46\u8fd9\u53ea\u6709\u5728 b \u4e2d\u53ea\u6709\u4e00\u4e2a a \u7684\u60c5\u51b5\u4e0b\u624d\u6709\u6548\u3002\u800c\u4e14\u4ecd\u7136\u65e0\u6cd5\u5b8c\u5168\u5b9e\u73b0\u6211\u7684\u613f\u671b\u3002<\/p>\n<p>\u9057\u61be\u7684\u662f\uff0cjson \u4e2d\u9700\u8981\u6b64\u7ed3\u6784\u7684\u539f\u56e0\u662f\u4e0d\u5bb9\u534f\u5546\u7684\u3002\u5e76\u4e14\u9700\u8981\u4f7f\u7528\u7684\u7ed3\u6784\u4f53\u4e2d\u4f1a\u5305\u542b\u5927\u91cf\u7684<code>a{}<\/code>\u3002\u5df2\u77e5\u5b83\u4eec\u5c06\u5305\u542b\u4ec0\u4e48\uff0c\u56e0\u6b64\u521b\u5efa\u548c\u4f7f\u7528\u5982\u4e0b\u7ed3\u6784\u662f\u4e00\u79cd\u9009\u62e9\u3002\u4f46\u968f\u7740\u9879\u76ee\u7684\u53d1\u5c55\uff0c\u5b83\u53ea\u4f1a\u9020\u6210\u6df7\u4e71\u5e76\u9020\u6210\u7ef4\u62a4\u5730\u72f1\u3002<\/p>\n<pre>type C struct {\n    p1 string `json:p1`\n    p2 int    `json:p1`\n    ...\n    a1   float64 `json:\"a1,omitempty\"`\n    a1_u string  `json:\"a1_u,omitempty\"`\n    a2   float64 `json:\"a2,omitempty\"`\n    a2_U string  `json:\"a2_u,omitempty\"`\n    ...\n}<\/pre>\n<p>\u53ef\u4ee5\u9009\u62e9\u4e3a <code>b{}<\/code> \u548c <code>a{}<\/code> \u7f16\u5199\u81ea\u5b9a\u4e49\u7f16\u7ec4\uff0c\u8fd9\u53ef\u80fd\u53ef\u4ee5\u5b8c\u6210\u6211\u7684\u9700\u8981\uff0c\u95ee\u9898\u662f\u6211\u4eec\u9700\u8981\u4e00\u5806\u4e0d\u540c\u7684\u7ed3\u6784\uff0c\u4f8b\u5982 <code>b{}<\/code> \uff0c\u5b83\u4eec\u8db3\u591f\u76f8\u4f3c\u6211\u76f8\u4fe1\u5e94\u8be5\u6709\u4e00\u79cd\u65b9\u6cd5\u6765\u6982\u62ec\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u8003\u8651\u4f7f\u7528\u4e0e\u53cd\u5c04\u76f8\u5173\u7684\u4e1c\u897f\u3002\u4f46\u7531\u4e8e\u6211\u5bf9 go \u8fd8\u5f88\u964c\u751f\uff0c\u6240\u4ee5\u6211\u8fd8\u6ca1\u6709\u627e\u5230\u89e3\u51b3\u65b9\u6848\u3002\u800c\u6211\u82b1\u5728\u8c37\u6b4c\u641c\u7d22\u4e0a\u7684\u6240\u6709\u52aa\u529b\u53ea\u80fd\u8ba9\u6211\u770b\u5230\u76f8\u53cd\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u63d0\u524d\u611f\u8c22\u60a8\u7684\u4efb\u4f55\u5e2e\u52a9\uff0c\u5982\u679c\u662f\u201c\u65e0\u6cd5\u5b8c\u6210\u201d\u3002<\/p>\n<p>*\u7f16\u8f91<\/p>\n<p><code>b<\/code> \u548c\u6269\u5c55 <code>c<\/code> \u610f\u5473\u7740\u80fd\u591f\u5305\u542b\u4efb\u610f\u6570\u91cf\u7684 <code>a<\/code> \u4ee5\u53ca\u4e00\u4e9b\u539f\u8bed\u3002\u5c06\u6765\u8fd8\u53ef\u4ee5\u6709 <code>d<\/code> \u548c <code>e<\/code> \u7c7b\u578b\uff0c\u6bcf\u4e2a\u7c7b\u578b\u90fd\u5305\u542b\u975e\u5e38\u89c4\u6570\u91cf\u7684 <code>a<\/code> \u548c\u57fa\u5143\u3002\u6216\u8005\u5b9e\u9645\u4e0a\u5177\u6709\u8fd9\u4e9b\u7ea6\u675f\u7684\u4efb\u610f\u6570\u91cf\u7684\u7ed3\u6784\u3002\u6211\u8bd5\u56fe\u907f\u514d\u7684\u662f\u4e3a\u6bcf\u4e2a\u65b0\u7ed3\u6784\u7f16\u5199\u4e00\u4e2a marhaller\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u7f16\u5199\u81ea\u5b9a\u4e49\u5c01\u9001\u62c6\u6536\u5668\u3002\u793a\u4f8b\uff1a<\/p>\n<pre>type A struct {\n    Value float64\n    Unit  string\n}\n\ntype B struct {\n    a1 A\n    a2 A\n}\n\nfunc (b *B) MarshalJSON() ([]byte, error) {\n    intermediate := map[string]interface{}{\n        \"a1\":   b.a1.Value,\n        \"a1_u\": b.a1.Unit,\n        \"a2\":   b.a2.Value,\n        \"a2_u\": b.a2.Unit,\n    }\n    return json.Marshal(intermediate)\n}\n<\/pre>\n<p>\u6839\u636e\u60a8\u671f\u671b\u6b64\u4ee3\u7801\u5982\u4f55\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\u800c\u589e\u957f\/\u6269\u5c55\uff0c\u60a8\u4e5f\u8bb8\u53ef\u4ee5\u4f7f\u7528\u67d0\u79cd\u5faa\u73af\u6216\u53cd\u5c04\uff0c\u4f46\u5728\u4e0d\u77e5\u9053\u6269\u5c55\u8ba1\u5212\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u53ef\u80fd\u5177\u4f53\u5316\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-207673","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207673","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=207673"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207673\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}