{"id":1256,"date":"2022-12-09T11:32:55","date_gmt":"2022-12-09T03:32:55","guid":{"rendered":"https:\/\/fdream.net\/blog\/?p=1256"},"modified":"2022-12-09T11:35:14","modified_gmt":"2022-12-09T03:35:14","slug":"json-unmarshal%e4%b8%8d%e5%8c%ba%e5%88%86%e5%a4%a7%e5%b0%8f%e5%86%99%e7%9a%84%e5%9d%91","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/1256","title":{"rendered":"json.Unmarshal\u4e0d\u533a\u5206\u5927\u5c0f\u5199\u7684\u5751"},"content":{"rendered":"\n<p>\u8fd9\u4e2a\u5751\u9690\u85cf\u5f97\u5f88\u6df1\uff0c\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u4e0d\u4f1a\u78b0\u5230\u3002\u5728\u8f6c\u6362JSON\u7684\u8fc7\u7a0b\u4e2d\uff0cUnmarshal\u4f1a\u6309\u5b57\u6bb5\u540d\u6765\u8f6c\u6362\uff0c\u78b0\u5230\u540c\u540d\u4f46\u5927\u5c0f\u5199\u4e0d\u4e00\u6837\u7684\u5b57\u6bb5\u4f1a\u5f53\u505a\u540c\u4e00\u5b57\u6bb5\u5904\u7406\u3002\u5728<a rel=\"noreferrer noopener\" href=\"https:\/\/pkg.go.dev\/encoding\/json#Unmarshal\" target=\"_blank\">\u5b98\u65b9\u6587\u6863<\/a>\u4e2d\u4ea6\u6709\u8bf4\u660e\uff08\u52a0\u7c97\u90e8\u5206\uff09\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but <strong>also accepting a case-insensitive match<\/strong>. By default, object keys which don&#8217;t have a corresponding struct field are ignored (see Decoder.DisallowUnknownFields for an alternative).<\/p>\n<\/blockquote>\n\n\n\n<p>\u7cbe\u786e\u5339\u914d\u6700\u597d\uff0c\u4f46\u662f\u4e5f\u63a5\u53d7\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u5339\u914d\u3002\u53ea\u80fd\u8bf4\u201c\u5305\u5bb9\u6027\u201d\u592a\u597d\u4e86\uff0c\u4f46\u6211\u8ba4\u4e3a\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u7684\u8bbe\u8ba1\u3002<\/p>\n\n\n\n<p>\u4e00\u6bb5\u5178\u578b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"encoding\/json\"\n\t\"fmt\"\n)\n\ntype UserInfo struct {\n\tUserID string `json:\"UserID\"`\n}\n\nfunc main() {\n\tvar u1 UserInfo\n\tjson.Unmarshal(&#91;]byte(`{\"UserID\":\"aaa\"}`), &amp;u1)\n\tfmt.Println(u1.UserID)  \/\/ \u6253\u5370 aaa\n\n\tvar u2 UserInfo\n\tjson.Unmarshal(&#91;]byte(`{\"UserID\":\"aaa\", \"UserId\":\"bbb\"}`), &amp;u2)\n\tfmt.Println(u2.UserID)  \/\/ \u6253\u5370 bbb\n\n\tvar u3 UserInfo\n\tjson.Unmarshal(&#91;]byte(`{\"UserID\":\"aaa\", \"UserId\":\"bbb\", \"userId\":\"ccc\"}`), &amp;u3)\n\tfmt.Println(u3.UserID)  \/\/ \u6253\u5370 ccc\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e2a\u5751\u9690\u85cf\u5f97\u5f88\u6df1\uff0c\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u4e0d\u4f1a\u78b0\u5230\u3002\u5728\u8f6c\u6362JSON\u7684\u8fc7\u7a0b\u4e2d\uff0cUnmarshal\u4f1a\u6309\u5b57\u6bb5\u540d\u6765\u8f6c\u6362\uff0c\u78b0\u5230\u540c\u540d\u4f46\u5927\u5c0f\u5199\u4e0d\u4e00\u6837\u7684\u5b57\u6bb5\u4f1a\u5f53\u505a\u540c\u4e00\u5b57\u6bb5\u5904\u7406\u3002\u5728\u5b98\u65b9\u6587\u6863\u4e2d\u4ea6\u6709\u8bf4\u660e\uff08\u52a0\u7c97\u90e8\u5206\uff09\uff1a To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match. By default, object keys which don&#8217;t have a corresponding struct field are ignored (see Decoder.DisallowUnknownFields for an alternative). \u7cbe\u786e\u5339\u914d\u6700\u597d\uff0c\u4f46\u662f\u4e5f\u63a5\u53d7\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u5339\u914d\u3002\u53ea\u80fd\u8bf4\u201c\u5305\u5bb9\u6027\u201d\u592a\u597d\u4e86\uff0c\u4f46\u6211\u8ba4\u4e3a\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u7684\u8bbe\u8ba1\u3002 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[374],"class_list":["post-1256","post","type-post","status-publish","format-standard","hentry","category-coding","tag-go"],"views":1274,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1256","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=1256"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/1256\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=1256"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=1256"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=1256"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}