@JsonProperty("id")
type Cyeam struct {
Url string `json:"url"`
Other string `json:"-"`
}
-
reflect
c := Cyeam{Url: "blog.cyeam.com", Other: "..."}
var t reflect.Type
t = reflect.TypeOf(c)
var v reflect.Value
v = reflect.ValueOf(c)
json := "{"
for i := 0; i < t.NumField(); i++ {
if t.Field(i).Tag.Get("json") != "-" {
json += "\"" + t.Field(i).Tag.Get("json") + "\":\"" + v.FieldByName(t.Field(i).Name).String() + "\""
}
}
json += "}"
fmt.Println(json)
{"url":"blog.cyeam.com"}
t.NumField()t.Field(i)t.Field(i).Tag.Get("json")
本文所涉及到的完整源码请参考。