这里我需要判断p.Email是否存在,如果存在,则指定EMAIL值,如果不指定空字符串
Email string"email"Email
nullEmail *stringifnil
当您需要区分未定义/空/空时,请使用下面答案中建议的自定义反编组拆收器:
type String struct {
IsDefined bool
Value string
}
// This method will be automatically invoked by json.Unmarshal
// but only for values that were provided in the json, regardless
// of whether they were null or not.
func (s *String) UnmarshalJSON(d []byte) error {
s.IsDefined = true
if string(d) != "null" {
return json.Unmarshal(d, &s.Value)
}
return nil
}
StringstringIsDefined