我的Go结构是这样的:
type BaseModel struct {
Id string `json:"id"`
CreatedTime time.Time `json:"createdTime"`
UpdatedTime time.Time `json:"updatedTime"`
Deleted bool `json:"deleted"`
}
type Category struct {
BaseModel
Parent string `json:"parent"`
Name string `json:"name"`
IconClass string `json:"iconClass"`
Mark string `json:"mark"`
}
我希望将Category
转换为JSON,如下所示:
{
"id":"",
"deleted":"",
...
"parent":"",
"name":""
}
但是当我使用Go的json转换它时,它给了我这个:
{
"Id":"",
"Deleted":"",
...
"parent":"",
"name":"",
...
}
我现在该怎么办?
我在代码下使用转换:
// define
var menus []models.Category
// query from db
q.Filter("deleted__exact", false).All(&menus)
// serialize it
res, _ := json.Marshal(&menus)
s := string(res[:])
beego.Debug(s)

答案 0 :(得分:0)
请在此处添加您的转换代码。下面的代码工作正常。
type BaseModel struct {
Id string `json:"id"`
CreatedTime time.Time `json:"createdTime"`
UpdatedTime time.Time `json:"updatedTime"`
Deleted bool `json:"deleted"`
}
type Category struct {
BaseModel
Parent string `json:"parent"`
Name string `json:"name"`
IconClass string `json:"iconClass"`
Mark string `json:"mark"`
}
func main() {
data, err := json.Marshal(Category{})
if err != nil {
return
}
fmt.Println(string(data[:]))
}
输出:
{"id":"","createdTime":"0001-01-01T00:00:00Z","updatedTime":"0001-01-01T00:00:00Z","deleted":false,"parent":"","name":"","iconClass":"","mark":""}