(1).标准Json
type User struct { Name string `json:"name"` Age int `json:"age"` Sex int `json:"sex"` School string `json:"school"` } func main() { liYanRu := User{ Name: "李燕茹", Age: 18, Sex: 2, School: "兰州大学", } jsonStr, _ := json.Marshal(liYanRu) godump.Dump(string(jsonStr)) } { "name": "李燕茹", "age": 18, "sex": 2, "school": "兰州大学" }
(2).简单的忽略方式,加上-号
type User struct { Name string `json:"name"` Age int `json:"age"` Sex int `json:"sex"` School string `json:"-"` } { "name": "李燕茹", "age": 18, "sex": 2 }
(3).动态的忽略方式,加上omitempty,需要的地方手动设置值为空即可
type User struct { Name string `json:"name"` Age int `json:"age"` Sex int `json:"sex"` School string `json:"school,omitempty"` } func main() { liYanRu := User{ Name: "李燕茹", Age: 18, Sex: 2, School: "", } jsonStr, _ := json.Marshal(liYanRu) godump.Dump(string(jsonStr)) }