目前正在映射服务的输出,可以说,为其布尔类型自由地交换 0 和 false(以及 1 和 true)。有没有办法为内置的 encoding/json unmarshal 函数使用更宽松的解析器?我试过在 json 标签中添加 ,string 无济于事。
我想要的一个例子:
type MyType struct {
AsBoolean bool `json:"field1"`
AlsoBoolean bool `json:"field2"`
}
然后,给定输入 json:
{
"field1" : true,
"field2" : 1
}
结果结构将是:
obj := MyType{}
json_err := json.Unmarshal([]byte(input_json), &obj)
fmt.Printf("%v\n", obj.AsBoolean) //"true"
fmt.Printf("%v\n", obj.AlsoBoolean) //"true"
相关分类