加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang 奇葩的时间包之Json与struct对象之间互相转换,JsonDate

发布时间:2020-12-16 18:11:51 所属栏目:大数据 来源:网络整理
导读:本篇在项目中实用性极强,推荐珍藏 package main import ( "encoding/json" "fmt" "time" ) type JsonDate time.Time type JsonTime time.Time func (p *JsonDate) UnmarshalJSON(data []byte) error { local,err := time.ParseInLocation(`"2006-01-02"`,st

本篇在项目中实用性极强,推荐珍藏

package main


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

type JsonDate time.Time
type JsonTime time.Time

func (p *JsonDate) UnmarshalJSON(data []byte) error {
local,err := time.ParseInLocation(`"2006-01-02"`,string(data),time.Local)
*p = JsonDate(local)
return err
}

func (p *JsonTime) UnmarshalJSON(data []byte) error {
local,err := time.ParseInLocation(`"2006-01-02 15:04:05"`,time.Local)
*p = JsonTime(local)
return err
}

func (c JsonDate) MarshalJSON() ([]byte,error) {
data := make([]byte,0)
data = append(data,'"')
data = time.Time(c).AppendFormat(data,"2006-01-02")
data = append(data,'"')
return data,nil
}

func (c JsonTime) MarshalJSON() ([]byte,"2006-01-02 15:04:05")
data = append(data,nil
}

func (c JsonDate) String() string {
return time.Time(c).Format("2006-01-02")
}

func (c JsonTime) String() string {
return time.Time(c).Format("2006-01-02 15:04:05")
}

func Todate(in string) (out time.Time,err error) {
out,err = time.Parse("2006-01-02",in)
return out,err
}

func Todatetime(in string) (out time.Time,err = time.Parse("2006-01-02 15:04:05",err
}

type Human struct {
Id int64 `json:"id"`
Name string `json:"name"`
Birthday JsonTime `json:"birthday"` //注意:日期带时分秒这样的格式,日期部分yyyy-MM-dd不能使用带斜线的格式
}

type Employee struct {
Id int64 `json:"id"`
Name string `json:"name"`
Birthday JsonDate `json:"birthday"` //注意:日期格式yyyy-MM-dd,日期不能使用带斜线的格式
}

func main() {
now := JsonTime(time.Now())
fmt.Println("当前时间:",now)
fmt.Println("========================================================================")

Tom := `{"id":5,"name":"Tom","birthday":"2016-06-30 16:09:51"}`
human := new(Human)
//json使用struct对象来解码 ------ json转struct对象
err1 := json.Unmarshal([]byte(Tom),&human)
if err1 != nil {
fmt.Println(err1)
}
fmt.Println("json转struct对象: ",*human)
fmt.Println("struct对象时间字段:",human.Birthday)
fmt.Println(time.Time(human.Birthday))

fmt.Println("========================================================================")

//struct对象使用json编码 ------ struct对象转json
data1,_ := json.Marshal(human)
fmt.Println("struct对象转json:",string(data1))
fmt.Println("nn")

fmt.Println("***********************************************************************************************")
fmt.Println("***********************************************************************************************")
Jack := `{"id":8,"name":"Jack","birthday":"2016-06-30"}`
employee := Employee{}
//json使用struct对象来解码 ------ json转struct对象
err2 := json.Unmarshal([]byte(Jack),&employee)
if err2 != nil {
fmt.Println(err2)
}
fmt.Println("json转struct对象: ",employee)
fmt.Println("struct对象时间字段:",employee.Birthday)
fmt.Println(time.Time(employee.Birthday))

fmt.Println("========================================================================")

//struct对象使用json编码 ------ struct对象转json
data2,_ := json.Marshal(employee)
fmt.Println("struct对象转json:",string(data2))
fmt.Println("nn")

JsonData := `[{"id":15,"name":"Jason","birthday":"1986-06-14"},{"id":16,"name":"Tim","birthday":"1986-12-06"}]`
var empList []Employee //定义一个切片
//json使用struct对象来解码 ------ json转struct对象
err3 := json.Unmarshal([]byte(JsonData),&empList)
if err3 != nil {
fmt.Println(err3)
}
fmt.Println("json转struct对象: ",empList)
fmt.Println("struct对象时间字段:",empList[1].Birthday)
fmt.Println(time.Time(empList[1].Birthday))

fmt.Println("========================================================================")

employees := make([]Employee,0) //定义一个切片
var date time.Time
var err4 error
date,err4 = Todate("1986-06-14") //字符串转time.Time类型
if err4 != nil {
fmt.Println(err4)
return
}
jason := Employee{Id: 15,Name: "Jason",Birthday: JsonDate(date)} //Birthday: JsonDate(date),注意键值对赋值方式
date,err4 = Todate("1986-12-06") //字符串转time.Time类型
if err4 != nil {
fmt.Println(err4)
return
}
tim := Employee{Id: 16,Name: "Tim",Birthday: JsonDate(date)}
employees = append(employees,jason)
employees = append(employees,tim)
//struct对象使用json编码 ------ struct对象转json
data3,_ := json.Marshal(employees)
fmt.Println("struct对象集合转json:",string(data3))
fmt.Println("nn")
}



控制台打印输出:

2017/06/25 05:37:59 server.go:73: Using API v1 2017/06/25 05:37:59 debugger.go:97: launching process with args: [/root/code/go/src/contoso.org/hello/debug] API server listening at: 127.0.0.1:2345 2017/06/25 05:37:59 debugger.go:505: continuing 当前时间: 2017-06-25 05:37:59 ======================================================================== json转struct对象: {5 Tom 2016-06-30 16:09:51} struct对象时间字段: 2016-06-30 16:09:51 2016-06-30 16:09:51 +0800 CST ======================================================================== struct对象转json: {"id":5,"birthday":"2016-06-30 16:09:51"} *********************************************************************************************** *********************************************************************************************** json转struct对象: {8 Jack 2016-06-30} struct对象时间字段: 2016-06-30 2016-06-30 00:00:00 +0800 CST ======================================================================== struct对象转json: {"id":8,"birthday":"2016-06-30"} json转struct对象: [{15 Jason 1986-06-14} {16 Tim 1986-12-06}] struct对象时间字段: 1986-12-06 1986-12-06 00:00:00 +0800 CST ======================================================================== struct对象集合转json: [{"id":15,"birthday":"1986-12-06"}]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读