我有一个可以根据 HTTP 请求标头输出为 JSON 或 XML 的应用程序。我可以通过将正确的标签添加到我正在使用的结构中来实现正确的输出,但我不知道如何为 JSON 和 XML 指定标签。
例如,这序列化以纠正 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"`
}
...但这对任何一个都不起作用:
type Foo struct {
Id int64 `xml:"id,attr",json:"id"`
Version int16 `xml:"version,attr",json:"version"`
}