golang结构体json的时间格式化解决方案
梦朝思夕 · · 1655 次点击 · · 开始浏览使用了OSC这么久了还没有写一篇博文,真实惭愧!在次写下第一篇。
最近开发项目时候发现一个结构体的Json转换的时间格式问题。
即这种1993年01月01日T20: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月01日T20: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月01日T20: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输出格式与打印方式。
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
使用了OSC这么久了还没有写一篇博文,真实惭愧!在次写下第一篇。
最近开发项目时候发现一个结构体的Json转换的时间格式问题。
即这种1993年01月01日T20: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月01日T20: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月01日T20: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输出格式与打印方式。