dongta5621 2016-01-20 19:48 采纳率: 0%
浏览 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?