golang结构体json的时间格式化解决方案

最近开发项目时候发现一个结构体的Json转换的时间格式问题。 即这种2019-07-01T20:08:23.000000028+08:00 这种表示UTC方法。从咱们习惯来讲,更喜欢但愿的是 2019-07-01 20:08:23这种格式。 从新复现代码以下:json

经过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 }this

type Info struct { Name string json:"name" InfoTime JsonTime json:"infotime" } func main() {code

info:=Info{
    Name:"qiangmzsx",
    InfoTime: JsonTime(time.now()),
}
b1,err:=json.Marshal(info)
if err!=nil {
    println(err)
}

println(string(b1))//{"name":"qiangmzsx","infotime":"2019-07-01 20:08:23"}

}orm

相关文章
相关标签/搜索