您将必须在自定义类型上实现
json.Marshaler/
json.Unmarshaler接口,并改用一个示例:
type CustomTime struct { time.Time}const ctLayout = "2006/01/02|15:04:05"func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) { s := strings.Trim(string(b), """) if s == "null" { ct.Time = time.Time{} return } ct.Time, err = time.Parse(ctLayout, s) return}func (ct *CustomTime) MarshalJSON() ([]byte, error) { if ct.Time.UnixNano() == nilTime { return []byte("null"), nil } return []byte(fmt.Sprintf(""%s"", ct.Time.Format(ctLayout))), nil}var nilTime = (time.Time{}).UnixNano()func (ct *CustomTime) IsSet() bool { return ct.UnixNano() != nilTime}type Args struct { Time CustomTime}var data = ` {"Time": "2014/08/01|11:27:18"}`func main() { a := Args{} fmt.Println(json.Unmarshal([]byte(data), &a)) fmt.Println(a.Time.String())}编辑 :添加
CustomTime.IsSet()以检查它是否实际设置,以供将来参考。



