我有一个应用程序,可以根据HTTP请求头输出JSON或XML.通过将正确的标记添加到我使用的 struct 中,我可以实现这两种方法的正确输出,但我不知道如何为JSON和XML指定标记.
For example, this serializes to correct XML:
type Foo struct {
Id int64 `xml:"id,attr"`
Version int16 `xml:"version,attr"`
}
...这将生成正确的JSON:
type Foo struct {
Id int64 `json:"id"`
Version int16 `json:"version"`
}
...but this doesn't work for either:
type Foo struct {
Id int64 `xml:"id,attr",json:"id"`
Version int16 `xml:"version,attr",json:"version"`
}