我有一个可以根据 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"`
}
温温酱
相关分类