{"id":206071,"date":"2025-06-12T11:47:05","date_gmt":"2025-06-12T03:47:05","guid":{"rendered":"https:\/\/server.hk\/cnblog\/206071\/"},"modified":"2025-06-12T11:47:05","modified_gmt":"2025-06-12T03:47:05","slug":"json-%e8%bd%ac-struct-%e6%97%b6%ef%bc%8c%e5%a6%82%e4%bd%95%e5%b0%86%e5%ad%97%e7%ac%a6%e4%b8%b2%e8%bd%ac%e6%8d%a2%e4%b8%ba-time-duration%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/206071\/","title":{"rendered":"JSON \u8f6c Struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.Duration\uff1f"},"content":{"rendered":"<p>\u5bf9\u4e8e\u4e00\u4e2aGOLANG\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cGOLANG\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aJSON \u8f6c Struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.Duration\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01,<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241102\/17305342746725db824546b.jpg\" class=\"aligncenter\">,<br \/>\n<strong>json \u8f6c struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.duration\uff1f<\/strong>,\u5728 go \u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u4f1a\u9047\u5230\u5c06 json \u6570\u636e\u8f6c\u6362\u6210 struct \u7684\u573a\u666f\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.duration \u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a,<br \/>\n<strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 int64 \u7c7b\u578b<\/strong>,time.duration \u662f\u4e00\u4e2a int64 \u7c7b\u578b\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 int64 \u7c7b\u578b\u7684\u5b57\u6bb5\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u8868\u793a\u7684 time.duration\u3002,<br \/>\n<strong>\u65b9\u6cd5 2\uff1a\u81ea\u5b9a\u4e49 struct \u5e76\u5b9e\u73b0 unmarshaljson<\/strong>,\u5982\u679c\u5e0c\u671b\u4f7f\u7528 time.duration \u7c7b\u578b\u7684\u5b57\u6bb5\uff0c\u4f46\u65e0\u6cd5\u4fee\u6539\u539f\u59cb struct \u5b9a\u4e49\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49 struct \u5e76\u5b9e\u73b0 unmarshaljson \u65b9\u6cd5\u3002,\u4eca\u5929\u5173\u4e8e\u300aJSON \u8f6c Struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.Duration\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01,json \u8f6c struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.duration\uff1f,\u5bf9\u4e8e\u4e00\u4e2aGOLANG\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cGOLANG\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aJSON \u8f6c Struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.Duration\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01,<br \/>\n<b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>JSON \u8f6c Struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.Duration\uff1f<\/span><\/p>\n<h1>JSON \u8f6c Struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.Duration\uff1f<\/h1>\n<p><span>2024-11-02 15:58:09<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5bf9\u4e8e\u4e00\u4e2aGOLANG\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7262\u56fa\u624e\u5b9e\u7684\u57fa\u7840\u662f\u5341\u5206\u91cd\u8981\u7684\uff0cGOLANG\u5c31\u6765\u5e26\u5927\u5bb6\u4e00\u70b9\u70b9\u7684\u638c\u63e1\u57fa\u7840\u77e5\u8bc6\u70b9\u3002\u4eca\u5929\u672c\u7bc7\u6587\u7ae0\u5e26\u5927\u5bb6\u4e86\u89e3\u300aJSON \u8f6c Struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.Duration\uff1f\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e86\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u5feb\u70b9\u6536\u85cf\u8d77\u6765\u5427\uff0c\u5426\u5219\u9700\u8981\u65f6\u5c31\u627e\u4e0d\u5230\u4e86\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241102\/17305342746725db824546b.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>json \u8f6c struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.duration\uff1f<\/strong><\/p>\n<p>\u5728 go \u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u4f1a\u9047\u5230\u5c06 json \u6570\u636e\u8f6c\u6362\u6210 struct \u7684\u573a\u666f\u3002\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.duration \u7c7b\u578b\u3002\u4ee5\u4e0b\u662f\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u65b9\u6cd5 1\uff1a\u4f7f\u7528 int64 \u7c7b\u578b<\/strong><\/p>\n<p>time.duration \u662f\u4e00\u4e2a int64 \u7c7b\u578b\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528 int64 \u7c7b\u578b\u7684\u5b57\u6bb5\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u8868\u793a\u7684 time.duration\u3002<\/p>\n<pre>import \"time\"\n\ntype student struct {\n    tim int64 `json:\"time\"`\n}<\/pre>\n<p><strong>\u65b9\u6cd5 2\uff1a\u81ea\u5b9a\u4e49 struct \u5e76\u5b9e\u73b0 unmarshaljson<\/strong><\/p>\n<p>\u5982\u679c\u5e0c\u671b\u4f7f\u7528 time.duration \u7c7b\u578b\u7684\u5b57\u6bb5\uff0c\u4f46\u65e0\u6cd5\u4fee\u6539\u539f\u59cb struct \u5b9a\u4e49\uff0c\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49 struct \u5e76\u5b9e\u73b0 unmarshaljson \u65b9\u6cd5\u3002<\/p>\n<pre>import \"time\"\n\ntype MyDuration struct {\n    time.Duration\n}\n\nfunc (d *MyDuration) UnmarshalJSON(data []byte) error {\n    \/\/ \u89e3\u6790\u5b57\u7b26\u4e32\u4e3a time.Duration\n    duration, err := time.ParseDuration(string(data))\n    if err != nil {\n        return err\n    }\n    d.Duration = duration\n    return nil\n}\n\ntype Student struct {\n    Tim MyDuration `json:\"time\"`\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aJSON \u8f6c Struct \u65f6\uff0c\u5982\u4f55\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a time.Duration\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8GOLANG\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Go \u4e2d\u81ea\u5b9a\u4e49\u9519\u8bef\u7ed3\u6784\u4f53\u5982\u4f55\u5b9e\u73b0 Error() \u65b9\u6cd5\u5e76\u89e3\u51b3 PostMan \u8fd4\u56de 500 \u9519\u8bef\uff1f\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   CSS\u4e2d\u7684position\u5c5e\u6027\uff1a\u5982\u4f55\u7cbe\u7ec6\u63a7\u5236\u5143\u7d20\u4f4d\u7f6e\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8e\u4e00\u4e2aGOLANG\u5f00\u53d1\u8005\u6765\u8bf4\uff0c&#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-206071","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206071","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=206071"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/206071\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=206071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=206071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=206071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}