使用
json.RawMessage以“拖延”解组过程决定做某件事之前要确定原始字节:
var data = []byte(`{ "somefield1":"somevalue1", "somefield2": null}`)type Data struct { SomeField1 string SomeField2 json.RawMessage}func main() { d := &Data{} _ = json.Unmarshal(data, &d) fmt.Println(d.SomeField1) if len(d.SomeField2) > 0 { if string(d.SomeField2) == "null" { fmt.Println("somefield2 is there but null") } else { fmt.Println("somefield2 is there and not null") // Do something with the data } } else { fmt.Println("somefield2 doesn't exist") }}
参观游乐场https://play.golang.org/p/Wganpf4sbO
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)