{"id":207587,"date":"2025-07-08T10:13:44","date_gmt":"2025-07-08T02:13:44","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207587\/"},"modified":"2025-07-08T10:13:44","modified_gmt":"2025-07-08T02:13:44","slug":"golang-jsonmarshal-unix-%e6%97%b6%e9%97%b4%e6%88%b3","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207587\/","title":{"rendered":"golang jsonMarshal unix \u65f6\u95f4\u6233"},"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>golang jsonMarshal unix \u65f6\u95f4\u6233<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 19:30:29<\/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\u5c31\u6574\u7406\u5206\u4eab\u300agolang jsonMarshal unix \u65f6\u95f4\u6233\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\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>\u6211\u5c1d\u8bd5\u4f7f\u7528 json.unmarshal \u81ea\u5b9a\u4e49\u7c7b\u578b timestamp \u5e76\u9047\u5230\u7cbe\u5ea6\u95ee\u9898\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>type timestamp  int64\ntype d struct {\n    name string `json:\"name\"`\n    age  int    `json:\"age\"`\n    ts  timestamp   `json:\"ts\"`\n}\n\nfunc (d timestamp) marshaljson() ([]byte, error){\n    rs :=time.unix(int64(d),0).format(\"2006-01-02\")\n    js,er := json.marshal(rs)\n    return js,er\n}\nfunc (d *timestamp) unmarshaljson(data []byte) error{\n    var rs string\n    e := json.unmarshal(data,&amp;rs)\n    if e!=nil{\n        return e\n    }\n    t,er:= time.parse(\"2006-01-02\", rs)\n    if er!=nil{\n        return er\n    }\n    *d = timestamp(t.unixnano())\n    return nil\n}\nfunc main() {\n    d :=d{\n        name:\"ft\",\n        age:9,\n        ts: (timestamp)(time.now().unix()),\n    }\n    js,er:=json.marshal(d)\n    if er!=nil{\n        fmt.println(er.error())\n        return\n    }\n    fmt.println(\"result:\",string(js))\n\n    var d2 d\n    e:=json.unmarshal(js,&amp;d2)\n    if e!=nil{\n        fmt.println(er.error())\n        return\n    }\n    fmt.println(\"result:\",d2)\n    fmt.println(time.now().unix())\n    fmt.println(d2.ts)\n}<\/pre>\n<p>\u6211\u5f97\u5230\u7684\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>result: {\"name\":\"ft\",\"age\":9,\"ts\":\"2018-09-30\"}\nresult: {ft 9 1538265600000000000}\n1538272374\n1538265600000000000<\/pre>\n<p>\u5982\u60a8\u6240\u89c1\uff0c\u89e3\u7ec4\u65f6\u95f4\u6233\u6709\u4e00\u4e2a\u957f\u5c3e\u3002 1538265600000000000 \u5982\u4f55\u5904\u7406<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u62b1\u6b49\uff0c\u56e0\u4e3a\u8fd9\u662f unix() \u548c unixnano() \u4e4b\u95f4\u7684\u533a\u522b<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300agolang jsonMarshal unix \u65f6\u95f4\u6233\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-207587","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207587","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=207587"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207587\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}