Golang在一个字段中使用多个json标记名称 [英] Golang multiple json tag names for one field
本文介绍了Golang在一个字段中使用多个json标记名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Golang中,JSON结构标签可以使用多个名称吗?
It it possible in Golang to use more than one name for a JSON struct tag ?
type Animation struct {
Name string `json:"name"`
Repeat int `json:"repeat"`
Speed uint `json:"speed"`
Pattern Pattern `json:"pattern",json:"frames"`
}
推荐答案
See How to define multiple name tags in a struct on how you can define multiple tags on one struct field.
您也可以使用type Info map[string]interface{}
代替您的结构.
You can also use a type Info map[string]interface{}
instead of your struct.
或者您可以在结构中使用这两种类型,并创建方法Details()
,该方法将返回正确的模式.
Or you can use both types in your structure, and make method Details()
which will return right pattern.
type Animation struct {
Name string `json:"name"`
Repeat int `json:"repeat"`
Speed uint `json:"speed"`
Pattern Pattern `json:"pattern"`
Frame Pattern `json:"frames"`
}
func (a Animation) Details() Pattern {
if a.Pattern == nil {
return a.Frame
}
return a.Pattern
}
这篇关于Golang在一个字段中使用多个json标记名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文