冬季很冷 發表於 2020-7-31 14:32:00

Go语言如何将json时间格式化为dateime格式

<p></p><div class="toc"><div class="toc-container-header">目录</div><ul><li>问题</li><li>一、示例:原生time.Time的json输出为UTC格式</li><li>二、自定义结构体Datetime(缺点是需要手动转换类型)</li><li>三、自定义结构体Datetime+自定义临时结构体(最佳方案)</li></ul></div><p></p>
<h1 id="问题">问题</h1>
<p>我们知道go语言的time.Time类型在转为json的时候,输出的是UTC的时间,而我们绝大部分时候使用的都是Datetime格式。<br>
其实解决问题的核心就是使用自定义字段,实现json的MarshaJSON方法</p>
<h1 id="一示例原生timetime的json输出为utc格式">一、示例:原生time.Time的json输出为UTC格式</h1>
<p>例如,我们有一个商品结构体Good,里边的CreatedAt和UpdateAt为time.Time类型</p>
<pre><code>package main

import (
        "encoding/json"
        "fmt"
        "time"
)

type Good struct {
        ID      int       `json:"id"`
        Name      string    `json:"name"`
        CreatedAt time.Time `json:"created_at"`
        UpdatedAt time.Time `json:"updated_at"`
}

func main() {
        good := Good{123, "chenqiognhe", time.Now(), time.Now()}
        bytes, _ := json.Marshal(good)
        fmt.Printf("%s", bytes)
}
</code></pre>
<p>输出结果为</p>
<pre><code class="language-json">{
        "id": 123,
        "name": "chenqiognhe",
        "created_at": "2020-07-31T14:27:10.035542+08:00",
        "updated_at": "2020-07-31T14:27:10.035542+08:00"
}
</code></pre>
<p>可以看到created_at和updated_at都是UTC的格式</p>
<h1 id="二自定义结构体datetime缺点是需要手动转换类型">二、自定义结构体Datetime(缺点是需要手动转换类型)</h1>
<p>如下,我们定义了一个time.Time的别名结构体Datetime</p>
<pre><code>package main

import (
        "encoding/json"
        "fmt"
        "time"
)

type Datetime time.Time

func (t Datetime) MarshalJSON() ([]byte, error) {
        var stamp = fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02 15:04:05"))
        return []byte(stamp), nil
}

type Good struct {
        ID      int      `json:"id"`
        Name      string   `json:"name"`
        CreatedAt Datetime `json:"created_at"`
        UpdatedAt Datetime `json:"updated_at"`
}

func main() {
        good := Good{123, "chenqiognhe", Datetime(time.Now()), Datetime(time.Now())}
        bytes, _ := json.Marshal(good)
        fmt.Printf("%s", bytes)
}
</code></pre>
<p>输出如下</p>
<pre><code class="language-json">{
        "id": 123,
        "name": "chenqiognhe",
        "created_at": "2020-07-31 14:27:39",
        "updated_at": "2020-07-31 14:27:39"
}
</code></pre>
<h1 id="三自定义结构体datetime自定义临时结构体最佳方案">三、自定义结构体Datetime+自定义临时结构体(最佳方案)</h1>
<pre><code>package main

import (
        "encoding/json"
        "fmt"
        "time"
)

type DateTime time.Time

func (t DateTime) MarshalJSON() ([]byte, error) {
        var stamp = fmt.Sprintf("\"%s\"", time.Time(t).Format("2006-01-02 15:04:05"))
        return []byte(stamp), nil
}

type Good struct {
        ID      int       `json:"id"`
        Name      string    `json:"name"`
        CreatedAt time.Time `json:"created_at"`
        UpdatedAt time.Time `json:"updated_at"`
}

func (t Good) MarshalJSON() ([]byte, error) {
        type TmpJSON Good
        return json.Marshal(&amp;struct {
                TmpJSON
                CreatedAt DateTime `json:"created_at"`
                UpdatedAt DateTime `json:"updated_at"`
        }{
                TmpJSON:   (TmpJSON)(t),
                CreatedAt: DateTime(t.CreatedAt),
                UpdatedAt: DateTime(t.UpdatedAt),
        })
}

func main() {
        good := Good{123, "chenqiognhe", time.Now(), time.Now()}
        bytes, _ := json.Marshal(good)
        fmt.Printf("%s", bytes)
}
</code></pre>
<p>这里我们给Good加了一个MarshalJSON方法实现,并通过临时结构体TmpJSON,指定了CreatedAt和UpdatedAt字段为Datetime类型。<br>
使用Good结构体,无需转换time.Time为Datetime,可以直接使用time.Time的所有方法。</p>
<p>输出结果为</p>
<pre><code class="language-json">{
        "id": 123,
        "name": "chenqiognhe",
        "created_at": "2020-07-31 14:28:12",
        "updated_at": "2020-07-31 14:28:12"
}
</code></pre><br><br>
来源:https://www.cnblogs.com/chenqionghe/p/13409556.html
頁: [1]
查看完整版本: Go语言如何将json时间格式化为dateime格式