{"id":208192,"date":"2025-07-08T13:59:47","date_gmt":"2025-07-08T05:59:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/208192\/"},"modified":"2025-07-08T13:59:47","modified_gmt":"2025-07-08T05:59:47","slug":"%e5%b0%86%e6%9c%89%e7%ac%a6%e5%8f%b7%e5%80%bc%e8%af%bb%e5%85%a5%e6%97%a0%e7%ac%a6%e5%8f%b7%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208192\/","title":{"rendered":"\u5c06\u6709\u7b26\u53f7\u503c\u8bfb\u5165\u65e0\u7b26\u53f7\u7c7b\u578b"},"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>\u5c06\u6709\u7b26\u53f7\u503c\u8bfb\u5165\u65e0\u7b26\u53f7\u7c7b\u578b<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-30 09:06:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5c06\u6709\u7b26\u53f7\u503c\u8bfb\u5165\u65e0\u7b26\u53f7\u7c7b\u578b\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\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\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728 go \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\u5f3a\u5236\u8bed\u8a00\u5c06\u5176\u89e3\u91ca\u4e3a\u65e0\u7b26\u53f7\u503c\uff1a<\/p>\n<pre>var x int32\nvar y uint32\nx = -1\ny = uint32(x)\n<\/pre>\n<p>\u8fd9\u6309\u9884\u671f\u5de5\u4f5c\uff0c<code>y<\/code> \u53d6\u6700\u5927\u7684\u65e0\u7b26\u53f7 int32 \u503c\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u6709\u4e00\u4e2a\u50cf\u8fd9\u6837\u7684 json \u5b57\u7b26\u4e32\uff1a<\/p>\n<p><em>\u6ce8\u610f\uff1a<\/em>\u503c <code>-20<\/code> \u5e8f\u5217\u5316\u4e3a\u5b57\u7b26\u4e32\u8fd8\u662f\u6570\u5b57\u4f3c\u4e4e\u5e76\u4e0d\u91cd\u8981<\/p>\n<pre>jsonStr := `{\"RandomNumber: -20}\n\ntype DummyJson struct {\n    RandomNumber uint32\n}\n\nrd := &amp;DummyJson{}\nerr := json.Unmarshal([]byte(jsonStr), &amp;rd)\nfmt.Println(err)\n<\/pre>\n<p>\u5728\u8fd9\u91cc\uff0c\u5b83\u62b1\u6028\u503c -20 \u65e0\u6cd5\u88ab\u89e3\u7ec4\u5230 randomnumber \u5b57\u6bb5\u4e2d\u3002\u6211\u600e\u6837\u624d\u80fd\u8bf4\u670dgo\u5c06\u6709\u7b26\u53f7\u7684\u503c\u63a8\u5165\u65e0\u7b26\u53f7\u7684\u7c7b\u578b\uff1f<\/p>\n<p>p.s\uff1a\u8fd9\u6837\u505a\u7684\u52a8\u673a\u65e2\u6f2b\u957f\u53c8\u590d\u6742\u3002\u56e0\u6b64\uff0c\u8bf7\u5047\u8bbe\u8fd9\u4e2a\u95ee\u9898\u80cc\u540e\u6709\u4e00\u4e2a\u5408\u7406\u7684\u7406\u7531\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u4f7f\u7528 <code>unmarshaljson<\/code> \u65b9\u6cd5\u521b\u5efa\u81ea\u5b9a\u4e49\u7c7b\u578b\u3002<\/p>\n<pre>type dummyjson struct {\n    randomnumber custominttype\n}\n\ntype custominttype uint32\n\nfunc (ct *custominttype) unmarshaljson(b []byte) error {\n    var x int32\n    var y uint32\n    s := string(b)\n    if i, err := strconv.atoi(s); err == nil {\n        x = int32(i)\n        y = uint32(x)\n        *ct = custominttype(y)\n    } else {\n        return err\n    }\n    return nil\n}\n<\/pre>\n<pre>jsonStr := `{\"RandomNumber\": -20}`\n\nvar rd DummyJson\nerr := json.Unmarshal([]byte(jsonStr), &amp;rd)\nfmt.Println(rd.RandomNumber, err)\n<\/pre>\n<p>\u6211\u786e\u4fe1\u53ef\u4ee5\u4f18\u5316\u8f6c\u6362\uff0c\u4f46\u4e0d\u786e\u5b9a\u5982\u4f55\u4f18\u5316\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u5c06\u6709\u7b26\u53f7\u503c\u8bfb\u5165\u65e0\u7b26\u53f7\u7c7b\u578b\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-208192","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208192","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=208192"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208192\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}