doushangan3690 2016-08-06 22:07
浏览 2497
已采纳

Golang在一个字段中使用多个json标记名称[重复]

This question already has an answer here:

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"`
}
</div>
  • 写回答

2条回答 默认 最新

  • douke1905 2016-08-06 22:24
    关注

    See How to define multiple name tags in a struct on how you can define multiple tags on one struct field.

    You can also use a type Info map[string]interface{} instead of your struct.

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏