{"id":42796,"date":"2024-12-01T13:38:54","date_gmt":"2024-12-01T05:38:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42796\/"},"modified":"2024-12-01T13:38:54","modified_gmt":"2024-12-01T05:38:54","slug":"%e4%bd%bf%e7%94%a8-golang-%e7%94%9f%e6%88%90%e5%b5%8c%e5%a5%97-json","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42796\/","title":{"rendered":"\u4f7f\u7528 golang \u751f\u6210\u5d4c\u5957 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>\u4f7f\u7528 golang \u751f\u6210\u5d4c\u5957 JSON<\/span><\/p>\n<h1>\u4f7f\u7528 golang \u751f\u6210\u5d4c\u5957 JSON<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 09:42:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4f7f\u7528 golang \u751f\u6210\u5d4c\u5957 JSON\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u90a3\u4e48\u6211\u5728\u8fd9\u91cc\u505a\u9519\u4e86\u4ec0\u4e48\uff0c\u6211\u5e76\u6ca1\u6709\u771f\u6b63\u7406\u89e3\u5982\u4f55\u751f\u6210\u5d4c\u5957\u7684 json \u7ed3\u6784\uff1a<\/p>\n<p>\u6211\u6536\u5230\u201c\u590d\u5408\u6587\u5b57\u4e2d\u7f3a\u5c11\u7c7b\u578b\u201d\u5f02\u5e38\u3002 https:\/\/play.golang.org\/p\/pa1fpbqhbb0<\/p>\n<pre>package main\n\nimport \"fmt\"\n\ntype FamilyRequestBody struct {\n    Family string `json:\"family\"`\n}\n\ntype DataRequestBody struct {\n    Family FamilyRequestBody `json:\"family\"`\n}\n\ntype EventRequestBody struct {\n    Account string `json:\"account\"`\n    Player  string `json:\"player\"`\n    Count   int    `json:\"count\"`\n}\n\ntype TeamRequestBody struct {\n    Account string `json:\"account\"`\n    Team    string `json:\"team\"`\n}\n\ntype PlayerRequestBody struct {\n    Account string          `json:\"account\"`\n    Team    string          `json:\"team\"`\n    Player  string          `json:\"player\"`\n    Data    DataRequestBody `json:\"data\"`\n}\n\nfunc main() {\n    l := PlayerRequestBody{\n        Account: \"my-account\",\n        Team:    \"12345\",\n        Player:  \"23424234\",\n        Data:    {Family: \"12345\"},\n    }\n\n    fmt.Printf(\"%#v\\n\", l)\n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u5b83\u975e\u5e38\u7b80\u5355\uff0c\u60a8\u662f\u8fd9\u6837\u5b9a\u4e49\u7684\uff1a<\/p>\n<pre>type playerrequestbody struct {\n    account string          `json:\"account\"`\n    team    string          `json:\"team\"`\n    player  string          `json:\"player\"`\n    data    datarequestbody `json:\"data\"`\n\n l := playerrequestbody{\n        account: \"my-account\",\n        team:    \"12345\",\n        player:  \"23424234\",\n        data:    datarequestbody{familyrequestbody{family:\"hello\"}},\n    }<\/pre>\n<p>\u4f46\u5e0c\u671b\u5b83\u50cf\u8fd9\u6837\u4f7f\u7528\u5b83\uff1a<\/p>\n<pre>type PlayerRequestBody struct {\n    Account string           `json:\"account\"`\n    Team    string           `json:\"team\"`\n    Player  string           `json:\"player\"`\n    Data    FamilyRequestBody`json:\"family\"`\n\n l := PlayerRequestBody{\n        Account: \"my-account\",\n        Team:    \"12345\",\n        Player:  \"23424234\",\n        Data:    {Family:\"hello\"}},\n    }<\/pre>\n<p>\u60a8\u7684\u5b57\u6bb5 <code>data<\/code> \u6765\u81ea\u7c7b\u578b <code>datarequestbody<\/code> \u5c31\u50cf\u60a8\u7684\u7ed3\u6784\u4e2d\u5b9a\u4e49\u7684\u90a3\u6837\uff0c\u60a8\u4e0d\u80fd\u5728\u90a3\u91cc\u653e\u7f6e\u53e6\u4e00\u4e2a\u7ed3\u6784\u3002\u5728 golang \u4e2d\u6ca1\u6709\u7ee7\u627f\u4e4b\u7c7b\u7684\u4e1c\u897f\uff0c\u8fd9\u53ef\u80fd\u4e5f\u4e0d\u662f\u60a8\u60f3\u8981\u7684\u3002<\/p>\n<p>\u6211\u540c\u610fop\u7684\u95ee\u9898\u4e0ejson\u65e0\u5173\uff0c\u5c31\u50cf\u8bc4\u8bba\u4e2d\u63d0\u5230\u7684f\u200b\u200blimzy\u4e00\u6837\uff0c\u4f46\u6211\u7559\u4e0b\u4e86\u826f\u597d\u63aa\u65bd\u7684\u94fe\u63a5\u3002<\/p>\n<p>\u5982\u679c\u8fd9\u4e0d\u662f\u60a8\u60f3\u8981\u7684\uff0c\u8bf7\u67e5\u770b\u8bb8\u591a\u5176\u4ed6\u95ee\u9898\u6765\u89e3\u91ca go \u4e2d\u7684 json \u5982\u4f55\u5de5\u4f5c\uff0c\u6216\u8005\u4f7f\u7528  \u6216 \u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u4f7f\u7528 golang \u751f\u6210\u5d4c\u5957 JSON\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u4f7f\u7528 golang \u751f\u6210\u5d4c\u5957 JSON \u4f7f\u7528 golang \u751f\u6210\u5d4c\u5957 JSON \u6765\u6e90\uff1astackoverflow 2024-04-26 09:42:34 0\u6d4f\u89c8 \u6536\u85cf \u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4f7f\u7528 golang \u751f\u6210\u5d4c\u5957 JSON\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002 \u95ee\u9898\u5185\u5bb9 \u90a3\u4e48\u6211\u5728\u8fd9\u91cc\u505a\u9519\u4e86\u4ec0\u4e48\uff0c\u6211\u5e76\u6ca1\u6709\u771f\u6b63\u7406\u89e3\u5982\u4f55\u751f\u6210\u5d4c\u5957\u7684 json \u7ed3\u6784\uff1a \u6211\u6536\u5230\u201c\u590d\u5408\u6587\u5b57\u4e2d\u7f3a\u5c11\u7c7b\u578b\u201d\u5f02\u5e38\u3002 https:\/\/play.golang.org\/p\/pa1fpbqhbb0 package main import &#8220;fmt&#8221; type FamilyRequestBody struct { Family string `json:&#8221;family&#8221;` } type DataRequestBody struct { Family FamilyRequestBody `json:&#8221;family&#8221;` } type EventRequestBody struct { Account string `json:&#8221;account&#8221;` [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42796","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42796","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=42796"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42796\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}