1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

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

type Student struct {
Id int64 `json:"id,string"`
Name string `json:"name"`
CreateTime MyTime `json:"create_time"`
UpdateTime time.Time `json:"update_time"`
}
func main() {
s1 := Student{Id:1,Name: "xmge"}
b,err := json.Marshal(s1)
fmt.Println(string(b),err)

s2 := new(Student)
err = json.Unmarshal(b,s2)
fmt.Println(s2,err)

b2,err := json.Marshal(s2)
fmt.Println(string(b2),err)
}

type MyTime time.Time

func (mt MyTime)MarshalJSON() ([]byte,error) {
t := time.Time(mt)
if t.IsZero() {
return []byte("null"),nil
}
return t.MarshalJSON()
}