{"id":207399,"date":"2025-07-08T12:42:19","date_gmt":"2025-07-08T04:42:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207399\/"},"modified":"2025-07-08T12:42:19","modified_gmt":"2025-07-08T04:42:19","slug":"%e4%bb%8e-unmarshaltypeerror-%e8%8e%b7%e5%8f%96%e5%a4%b1%e8%b4%a5%e5%ad%97%e6%ae%b5","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207399\/","title":{"rendered":"\u4ece UnmarshalTypeError \u83b7\u53d6\u5931\u8d25\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>\u4ece UnmarshalTypeError \u83b7\u53d6\u5931\u8d25\u5b57\u6bb5<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 11:36: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>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u4ece UnmarshalTypeError \u83b7\u53d6\u5931\u8d25\u5b57\u6bb5\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6839\u636e go stdlib\uff0c\u5f53 json \u5c5e\u6027\u7c7b\u578b\u4e0e\u7ed3\u6784\u4f53\u7c7b\u578b\u4e0d\u540c\u65f6\uff0c\u4f1a\u8fd4\u56de\u9519\u8bef\u3002\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/ an unmarshaltypeerror describes a json value that was\n\/\/ not appropriate for a value of a specific go type.\ntype unmarshaltypeerror struct {\n    value  string       \/\/ description of json value - \"bool\", \"array\", \"number -5\"\n    type   reflect.type \/\/ type of go value it could not be assigned to\n    offset int64        \/\/ error occurred after reading offset bytes\n    struct string       \/\/ name of the struct type containing the field\n    field  string       \/\/ name of the field holding the go value\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u5c1d\u8bd5\u901a\u8fc7\u5728\u7ed3\u6784\u4f53\u4e2d\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u6bb5\u5e76\u5411\u8be5\u5b57\u6bb5\u63d0\u4f9b int \u6765\u6a21\u62df\u7c7b\u578b\u8f6c\u6362\u5931\u8d25\u3002<\/p>\n<pre>import (\n    \"encoding\/json\"\n    \"fmt\"\n)\n\ntype Sample struct {\n    StringProp string `json:\"a_string\"`\n}\n\nfunc main(){\n    jsonString := `{ \"a_string\" : 1 }`\n    s := Sample{}\n    err := json.Unmarshal([]byte(jsonString), &amp;s)\n    if err != nil {\n        typeErr := err.(*json.UnmarshalTypeError)\n        fmt.Print(typeErr.Field)\n    }\n}<\/pre>\n<p>\u4f46\u4e0d\u5e78\u7684\u662f\uff0c\u8be5\u9519\u8bef\u6ca1\u6709\u4efb\u4f55\u201cstruct\u201d\u6216\u201cfield\u201d\u5c5e\u6027\u503c\u3002\u8fd9\u4e9b\u5c5e\u6027\u6709\u4ec0\u4e48\u7528\uff1f \u6709\u6ca1\u6709\u529e\u6cd5\u68c0\u6d4b\u54ea\u4e2a\u5c5e\u6027\u89e3\u7ec4\u5931\u8d25\uff1f<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u95ee\u9898\u4ec5\u5728\u6211\u7684\u672c\u5730\u73af\u5883\u4e2d\u91cd\u73b0\u3002\u5220\u9664golang\uff08\u6211\u7528brew\u5b89\u88c5\u4e863\u4e2a\u7248\u672c\uff09\u5e76\u518d\u6b21\u5b89\u88c5go\u540e\uff0c\u5b83\u5f00\u59cb\u6309\u9884\u671f\u5de5\u4f5c\u3002 <code>Struct<\/code> \u548c <code>Field<\/code> \u6b63\u5728\u518d\u6b21\u586b\u5145\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u4e2a<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4ece UnmarshalTypeError \u83b7\u53d6\u5931\u8d25\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-207399","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207399","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=207399"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207399\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}