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

使用了OSC这么久了还没有写一篇博文,真实惭愧!在此写下第一篇。
最近开发项目时候发现一个结构体的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输出格式与打印方式。

转载于:https://my.oschina.net/qiangmzsx/blog/1408330

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Go语言中,Protocol Buffers(protobuf)和JSON是两种常用的数据序列格式,它们可以帮助你在不同环境之间传输数据,比如服务间通信、持久存储等。以下是关于它们互转的简单介绍: 1. Protocol Buffers (protobuf): - **protobuf**是由Google开发的一种高效的跨语言数据交换格式,使用.proto文件定义数据结构,然后用`protoc`编译器生成对应的Go代码,实现了数据结构和编码解码功能。 - 在Go中,你可以使用`google/protobuf`包来处理protobuf数据,`pb.NewMessage()`创建一个新的protobuf实例,`Marshal()`和`Unmarshal()`方法分别用于序列和反序列。 2. JSON (JavaScript Object Notation): - **JSON**是一种轻量级的数据交换格式,易于人阅读和编写,也容易被机器解析和生成。Go语言中的`encoding/json`包提供了处理JSON数据的功能。 - Go中的`json.Marshal()`和`json.Unmarshal()`函数分别用于将Go数据结构编码为JSON字符串和从JSON字符串反序列到Go结构体。 互转方法: - **从protobuf转JSON**: - 首先,使用`protoc`编译器生成Go代码,包含`json`标签的protobuf字段可以直接转换。 - 如果没有直接支持,可以手动将protobuf对象转换为map[string]interface{},然后使用`json.Marshal()`。 - **从JSON转protobuf**: - 使用`json.Unmarshal()`将JSON字符串解码成Go map或struct,然后构造protobuf对象,设置相应的字段值。 相关问题: 1. protoc编译器在Go中如何使用? 2. 如何在protobuf中添加`json`标签以支持JSON序列? 3. 如果protobuf对象中某些字段不需要JSON转换,如何处理?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值