测试那些会被忽略
package main
import (
"encoding/json"
"fmt"
)
type Struct struct {
Map map[string]interface{} `json:"map,omitempty"`
MapPtr *map[string]interface{} `json:"map_ptr,omitempty"`
Slice []int `json:"slice,omitempty"`
SlicePtr []*int `json:"slice_ptr,omitempty"`
Str string `json:"str,omitempty"`
StrPtr *string `json:"str_ptr,omitempty"`
Bool bool `json:"bool,omitempty"`
BoolPtr *bool `json:"bool_ptr,omitempty"`
}
func main() {
var (
i = 1
b = true
s = ""
)
var obj = Struct{
Map: map[string]interface{}{
"a":"b",
},
MapPtr: &map[string]interface{}{},
Slice: []int{1},
SlicePtr: []*int{&i},
Str: "",
StrPtr: &s,
Bool: false,
BoolPtr: &b,
}
result, _ := json.Marshal(obj)
fmt.Println("result = ", string(result))
}
测试结果:
如果是slice ,空的会被忽略
如果是bool类型,false 会被忽略
如果是string类型,空字符串会被忽略
如果是map类型,空的会被忽略