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 我的数据无法存进链表里
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端