Golang: Having trouble with nested JSON Unmarshaler
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Given the following code: package main import ( "encoding/json" "log" ) type Somefin string func (s *Somefin) UnmarshalJSON(b []byte) error { log.Println("Unmarshaling",string(b)) *s = Somefin("~"+string(b)+"~") return nil } type Wat struct { A, B string *Somefin } func main() { b := []byte(`{"A":"foo","B":"bar","Somefin":"baz"}`) w := &Wat{Somefin: new(Somefin)} err := json.Unmarshal(b,w) log.Println(w, err) } I get the following output: # go run wat.go 2013/12/14 13:59:17 Unmarshaling {"A":"foo","B":"bar","Somefin":"baz"} 2013/12/14 13:59