json.Marshal(struct) returns “{}”

后端 未结 3 1623
Happy的楠姐
Happy的楠姐 2020-11-22 07:17
type TestObject struct {
    kind string `json:\"kind\"`
    id   string `json:\"id, omitempty\"`
    name  string `json:\"name\"`
    email string `json:\"email\"`
         


        
3条回答
  •  感动是毒
    2020-11-22 07:57

    You need to export the fields in TestObject by capitalizing the first letter in the field name. Change kind to Kind and so on.

    type TestObject struct {
     Kind string `json:"kind"`
     Id   string `json:"id,omitempty"`
     Name  string `json:"name"`
     Email string `json:"email"`
    }
    

    The encoding/json package and similar packages ignore unexported fields.

    The `json:"..."` strings that follow the field declarations are struct tags. The tags in this struct set the names of the struct's fields when marshaling to and from JSON.

    playground

提交回复
热议问题