{"id":207682,"date":"2025-07-08T10:07:15","date_gmt":"2025-07-08T02:07:15","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207682\/"},"modified":"2025-07-08T10:07:15","modified_gmt":"2025-07-08T02:07:15","slug":"%e6%a0%b9%e6%8d%ae%e5%af%86%e9%92%a5%e8%a7%a3%e7%bb%84-json","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207682\/","title":{"rendered":"\u6839\u636e\u5bc6\u94a5\u89e3\u7ec4 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>\u6839\u636e\u5bc6\u94a5\u89e3\u7ec4 JSON<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 16:45:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6700\u8fd1\u53d1\u73b0\u4e0d\u5c11\u5c0f\u4f19\u4f34\u90fd\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u5f88\u611f\u5174\u8da3\uff0c\u6240\u4ee5\u4eca\u5929\u7ee7\u7eed\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\uff0c\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u6839\u636e\u5bc6\u94a5\u89e3\u7ec4 JSON\u300b<\/span>\u4e3b\u8981\u5185\u5bb9\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u5230\u4f60\uff01\u5f53\u7136\u5982\u679c\u9605\u8bfb\u672c\u6587\u65f6\u5b58\u5728\u4e0d\u540c\u60f3\u6cd5\uff0c\u53ef\u4ee5\u5728\u8bc4\u8bba\u4e2d\u8868\u8fbe\uff0c\u4f46\u662f\u8bf7\u52ff\u4f7f\u7528\u8fc7\u6fc0\u7684\u63aa\u8f9e~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u4ece\u7f51\u7edc\u63a5\u6536 json \u683c\u5f0f\u7684\u6570\u636e\uff0c\u6211\u9700\u8981\u6839\u636e\u5bc6\u94a5\u5bf9\u5176\u8fdb\u884c\u89e3\u7ec4\u3002<\/p>\n<p> \u8fd9\u662f\u6570\u636e\u793a\u4f8b\uff1a<\/p>\n<pre>{\n  \"foo\": {\n    \"11883920\": {\n      \"fieldA\": 123,\n      \"fieldB\": [\n        {\n          \"fieldC\": \"a\",\n          \"fieldD\": 1173653.22\n        }\n      ]\n    }\n  },\n  \"bar\": {\n     \"123\": {\n       \"fieldE\": 123\n     }\n   }\n  \"anyOtherkey\": {...}\n}<\/pre>\n<p>\u903b\u8f91\u662f\uff0c\u5982\u679c\u5bc6\u94a5\u662f <code>foo<\/code>\uff0c\u5219\u5e94\u5c06\u5176\u89e3\u7ec4\u4e3a <code>foostruct<\/code>\uff0c\u5982\u679c <code>bar<\/code> &#8211; \u5219\u5e94\u89e3\u7ec4\u4e3a <code>barstruct<\/code>\u3002 \u5b9e\u73b0\u8fd9\u4e2a\u903b\u8f91\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f \uff08\u6211\u4e0d\u60f3\u5c06\u5176\u89e3\u7ec4\u5230 <code>map[string]interface{}<\/code>\uff0c\u4e5f\u8bb8\u53ef\u4ee5\u4f7f\u7528 <code>json.newdecoder()<\/code> \u51fd\u6570\uff0c\u4f46\u6211\u65e0\u6cd5\u83b7\u5f97\u9884\u671f\u7ed3\u679c\uff09\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u53ea\u9700\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5b57\u6bb5\u7684\u7c7b\u578b\uff1a<\/p>\n<pre>type mytype struct {\n    foo      *foostruct `json:\"foo,omitempty\"`\n    bar      *barstruct `json:\"bar,omitempty\"`\n    otherkey string     `json:\"other_key\"`\n}<\/pre>\n<p>\u5c06 json \u89e3\u7ec4\u4e3a\u8be5\u7c7b\u578b\uff0c\u7136\u540e\u53ea\u9700\u68c0\u67e5 ig <code>foo<\/code> \u548c\/\u6216 <code>bar<\/code> \u662f\u5426\u4e3a nil \u5373\u53ef\u4e86\u89e3\u60a8\u6b63\u5728\u4f7f\u7528\u7684\u6570\u636e\u3002<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6f14\u793a \uff0c\u5c55\u793a\u5b83\u7684\u6837\u5b50<\/p>\n<p>\u5176\u672c\u8d28\u662f\uff1a<\/p>\n<pre>type foo struct {\n    field int `json:\"field1\"`\n}\n\ntype bar struct {\n    message string `json:\"field2\"`\n}\n\ntype payload struct {\n    foo   *foo   `json:\"foo,omitempty\"`\n    bar   *bar   `json:\"bar,omitempty\"`\n    other string `json:\"another_field\"`\n}<\/pre>\n<p><code>foo<\/code> \u548c <code>bar<\/code> \u5b57\u6bb5\u662f\u6307\u9488\u7c7b\u578b\uff0c\u56e0\u4e3a\u503c\u5b57\u6bb5\u4f1a\u4f7f\u786e\u5b9a\u5b9e\u9645\u8bbe\u7f6e\u7684\u5b57\u6bb5\u53d8\u5f97\u66f4\u52a0\u9ebb\u70e6\u3002 <code>omitempty<\/code> \u4f4d\u5141\u8bb8\u60a8\u5c01\u9001\u76f8\u540c\u7c7b\u578b\u4ee5\u91cd\u65b0\u521b\u5efa\u539f\u59cb\u6709\u6548\u8d1f\u8f7d\uff0c\u56e0\u4e3a <code>nil<\/code> \u503c\u4e0d\u4f1a\u663e\u793a\u5728\u8f93\u51fa\u4e2d\u3002<\/p>\n<p>\u8981\u68c0\u67e5\u539f\u59cb json \u5b57\u7b26\u4e32\u4e2d\u8bbe\u7f6e\u4e86\u54ea\u4e2a\u5b57\u6bb5\uff0c\u53ea\u9700\u7f16\u5199\uff1a<\/p>\n<pre>var data Payload\nif err := json.Unmarshal([]byte(jsonString), &amp;data); err != nil {\n    \/\/ handle error\n}\nif data.Foo == nil &amp;&amp; data.Bar == nil {\n    \/\/ this is probably an error-case you need to handle\n}\nif data.Foo == nil {\n    fmt.Println(\"Bar was set\")\n} else {\n    fmt.Println(\"Foo was set\")\n}<\/pre>\n<p>\u4ec5\u6b64\u800c\u5df2<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u6839\u636e\u5bc6\u94a5\u89e3\u7ec4 JSON\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\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-207682","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207682","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=207682"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207682\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207682"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207682"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207682"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}