本篇在项目中实用性极强,推荐珍藏
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("\n\n")
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("\n\n")
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("\n\n")
}
控制台打印输出:
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"}]
总结以上是内存溢出为你收集整理的Golang 奇葩的时间包之Json与struct对象之间互相转换,JsonDate和JsonTime自定义类型如何实现json编码和解码(二)全部内容,希望文章能够帮你解决Golang 奇葩的时间包之Json与struct对象之间互相转换,JsonDate和JsonTime自定义类型如何实现json编码和解码(二)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)