dongta5621 2016-01-20 19:48
浏览 88
已采纳

如何在Go结构中覆盖json标签?

I'd like to marshal part of this struct:

type ValueSet struct {
    Id           string                       `json:"id" bson:"_id"`
    Url          string                       `bson:"url,omitempty" json:"url,omitempty"`
    Identifier   *Identifier                  `bson:"identifier,omitempty" json:"identifier,omitempty"`
    Version      string                       `bson:"version,omitempty" json:"version,omitempty"`
    Name         string                       `bson:"name,omitempty" json:"name,omitempty"`
    Status       string                       `bson:"status,omitempty" json:"status,omitempty"`
    Experimental *bool                        `bson:"experimental,omitempty" json:"experimental,omitempty"`
    Publisher    string                       `bson:"publisher,omitempty" json:"publisher,omitempty"`
    Contact      []ValueSetContactComponent   `bson:"contact,omitempty" json:"contact,omitempty"`
    Date         *FHIRDateTime                `bson:"date,omitempty" json:"date,omitempty"`
    LockedDate   *FHIRDateTime                `bson:"lockedDate,omitempty" json:"lockedDate,omitempty"`
    Description  string                       `bson:"description,omitempty" json:"description,omitempty"`
    UseContext   []CodeableConcept            `bson:"useContext,omitempty" json:"useContext,omitempty"`
    Immutable    *bool                        `bson:"immutable,omitempty" json:"immutable,omitempty"`
    Requirements string                       `bson:"requirements,omitempty" json:"requirements,omitempty"`
    Copyright    string                       `bson:"copyright,omitempty" json:"copyright,omitempty"`
    Extensible   *bool                        `bson:"extensible,omitempty" json:"extensible,omitempty"`
    CodeSystem   *ValueSetCodeSystemComponent `bson:"codeSystem,omitempty" json:"codeSystem,omitempty"`
    Compose      *ValueSetComposeComponent    `bson:"compose,omitempty" json:"compose,omitempty"`
    Expansion    *ValueSetExpansionComponent  `bson:"expansion,omitempty" json:"expansion,omitempty"`
}

which is part of a Go implementation of HL7 FHIR, including only the metadata fields, and omitting the three content three fields (codeSystem, compose and expansion). I can't (and shouldn't) change the JSON tags in the original source code, since other code strongly depends on it working the way it's written. How can I tell json.Marshal to override the existing JSON tags on these struct elements?

  • 写回答

1条回答 默认 最新

  • douchuo9476 2016-01-20 20:00
    关注

    You can't change it, but you don't have to.

    Easiest solution is to create your own struct, define your own json tags (how you want them to appear in the output), copy the fields, and marshal a value of your own struct.

    E.g. let's say you want to marshal the Id and Urlfields, then:

    type MyValueSet struct {
        Id string  `json:"MyId"`
        Url string `json:"MyUrl"`
    }
    
    var vs ValueSet = ... // Comes from somewhere
    
    mvs := MyValueSet {
        Id:  vs.Id,
        Url: vs.Url,
    }
    
    data, err := json.Marshal(&mvs)
    // Check err
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目