package main import ( "encoding/json" "errors" "fmt" ) type A struct { Br Boolean `json:"br"` } func main() { var c A e := json.Unmarshal([]byte(`{"br":true}`), &c) fmt.Println(e) fmt.Println(c) } type Boolean int func (b Boolean) MarshalJSON() ([]byte, error) { switch b { case 10: return []byte("true"), nil case -10: return []byte("false"), nil default: return []byte("null"), nil } } func (b *Boolean) UnmarshalJSON(data []byte) error { switch string(data) { case "true": *b = 10 case "false": *b = -10 case "null": *b = 0 default: return errors.New("不是正确类型") } return nil } func (b Boolean) IsNil() bool { return b == 0 } func (b Boolean) IsValue() bool { return b == 10 }
标签:10,false,golang,json,Boolean,func,return,byte From: https://www.cnblogs.com/hardykay/p/16633853.html