golang結構體json的時間格式化解決方案

qiangmzsx發表於2017-07-15

最近開發專案時候發現一個結構體的Json轉換的時間格式問題。 即這種1993-01-01T20:08:23.000000028+08:00 這種表示UTC方法。從我們習慣來說,更喜歡希望的是 1993-01-01 20:08:23這種格式。 重新復現程式碼如下:


package main

import (
    "time"
    "encoding/json"
)

type Student struct {
    Name string     `json:"name"`
    Brith time.Time `json:"brith"`
}

func main()  {
    stu:=Student{
        Name:"qiangmzsx",
        Brith:time.Date(1993, 1, 1, 20, 8, 23, 28, time.Local),
    }

    b,err:=json.Marshal(stu)
    if err!=nil {
        println(err)
    }

    println(string(b))//{"name":"qiangmzsx","brith":"1993-01-01T20:08:23.000000028+08:00"}
}

遇到這樣的問題,那麼Golang是如何解決的呢? 有兩種解決方案,下面我們一個個來看看。

通過time.Time型別別名

type JsonTime time.Time
// 實現它的json序列化方法
func (this JsonTime) MarshalJSON() ([]byte, error) {
    var stamp = fmt.Sprintf("\"%s\"", time.Time(this).Format("2006-01-02 15:04:05"))
    return []byte(stamp), nil
}
type Student1 struct {
    Name string     `json:"name"`
    Brith JsonTime  `json:"brith"`
}
func main()  {

    stu1:=Student1{
        Name:"qiangmzsx",
        Brith:JsonTime(time.Date(1993, 1, 1, 20, 8, 23, 28, time.Local)),
    }
    b1,err:=json.Marshal(stu1)
    if err!=nil {
        println(err)
    }

    println(string(b1))//{"name":"qiangmzsx","brith":"1993-01-01 20:08:23"}
}

使用結構體組合方式

相較於第一種方式,該方式顯得複雜一些,我也不是很推薦使用,就當做是一個擴充套件教程吧。

type Student2 struct {
    Name string     `json:"name"`
    // 一定要將json的tag設定忽略掉不解析出來
    Brith time.Time  `json:"-"`
}
// 實現它的json序列化方法
func (this Student2) MarshalJSON() ([]byte, error) {
    // 定義一個該結構體的別名
    type AliasStu Student2
    // 定義一個新的結構體
    tmpStudent:= struct {
        AliasStu
        Brith string `json:"brith"`
    }{
        AliasStu:(AliasStu)(this),
        Brith:this.Brith.Format("2006-01-02 15:04:05"),
    }
    return json.Marshal(tmpStudent)
}
func main()  {
    stu2:=Student2{
        Name:"qiangmzsx",
        Brith:time.Date(1993, 1, 1, 20, 8, 23, 28, time.Local),
    }

    b2,err:=json.Marshal(stu2)
    if err!=nil {
        println(err)
    }

    println(string(b2))//{"name":"qiangmzsx","brith":"1993-01-01 20:08:23"}
}

該方法使用了Golang的結構體的組合方式,可以實現OOP的繼承,也是體現Golang靈活。

整體程式碼

下面把上面的程式碼組成整體貼出來。


package main

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

type Student struct {
    Name string     `json:"name"`
    Brith time.Time `json:"brith"`
}

type JsonTime time.Time
// 實現它的json序列化方法
func (this JsonTime) MarshalJSON() ([]byte, error) {
    var stamp = fmt.Sprintf("\"%s\"", time.Time(this).Format("2006-01-02 15:04:05"))
    return []byte(stamp), nil
}
type Student1 struct {
    Name string     `json:"name"`
    Brith JsonTime  `json:"brith"`
}

type Student2 struct {
    Name string     `json:"name"`
    // 一定要將json的tag設定忽略掉不解析出來
    Brith time.Time  `json:"-"`
}
// 實現它的json序列化方法
func (this Student2) MarshalJSON() ([]byte, error) {
    // 定義一個該結構體的別名
    type AliasStu Student2
    // 定義一個新的結構體
    tmpStudent:= struct {
        AliasStu
        Brith string `json:"brith"`
    }{
        AliasStu:(AliasStu)(this),
        Brith:this.Brith.Format("2006-01-02 15:04:05"),
    }
    return json.Marshal(tmpStudent)
}

func main()  {
    stu:=Student{
        Name:"qiangmzsx",
        Brith:time.Date(1993, 1, 1, 20, 8, 23, 28, time.Local),
    }

    b,err:=json.Marshal(stu)
    if err!=nil {
        println(err)
    }

    println(string(b))//{"name":"qiangmzsx","brith":"1993-01-01T20:08:23.000000028+08:00"}

    println("===================")

    stu1:=Student1{
        Name:"qiangmzsx",
        Brith:JsonTime(time.Date(1993, 1, 1, 20, 8, 23, 28, time.Local)),
    }
    b1,err:=json.Marshal(stu1)
    if err!=nil {
        println(err)
    }

    println(string(b1))//{"name":"qiangmzsx","brith":"1993-01-01 20:08:23"}

    println("===================")
    stu2:=Student2{
        Name:"qiangmzsx",
        Brith:time.Date(1993, 1, 1, 20, 8, 23, 28, time.Local),
    }

    b2,err:=json.Marshal(stu2)
    if err!=nil {
        println(err)
    }

    println(string(b2))//{"name":"qiangmzsx","brith":"1993-01-01 20:08:23"}
}

值得一提的是,對任意struct增加 MarshalJSON ,UnmarshalJSON , String 方法,實現自定義json輸出格式與列印方式。

相關文章