1. 程式人生 > >golang結構體json的時間格式化解決方案

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

結構體 json golang

最近開發項目時候發現一個結構體的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輸出格式與打印方式。

本文出自 “夢朝思夕” 博客,請務必保留此出處http://qiangmzsx.blog.51cto.com/2052549/1947844

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