我们需要对嵌套在多个其他结构中的结构使用自定义解组器,而这些结构不需要自定义解组器。我们有很多与B下面定义的结构类似的结构(类似于嵌套A)。代码的输出是true false 0(预期的true false 2)。有任何想法吗?
Go Playground 示例在这里。
package main
import (
"fmt"
"encoding/json"
)
type A struct {
X bool `json:"x"`
Y bool `json:"y"`
}
type B struct {
A
Z int `json:"z"`
}
func (a *A) UnmarshalJSON(bytes []byte) error {
var aa struct {
X string `json:"x"`
Y string `json:"y"`
}
json.Unmarshal(bytes, &aa)
a.X = aa.X == "123"
a.Y = aa.Y == "abc"
return nil
}
const myJSON …Run Code Online (Sandbox Code Playgroud)