作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在构建一个 API 来处理 POST 中的 XML 数据,并在响应中返回它。应该处理此编码(marshal)处理的函数正在接收类型为 map[string]interface{}
的变量数据,它看起来像这样(如果您将其记录到控制台):
map[lala:success blabla:0xc42011e700 status:true]
复制适用于 JSON 编码(marshal)处理的步骤或尝试遵循文档让我陷入了死胡同。这是我到目前为止的代码:
type Map map[string]interface{}
type xmlMapEntry struct {
XMLName xml.Name
Value string `xml:",chardata"`
}
func (m Map) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
for k, v := range m {
e.Encode(xmlMapEntry{XMLName: xml.Name{Local: k}, Value: v})
}
return e.EncodeToken(start.End())
}
当 e.Encode 行返回时,我陷入了死胡同:cannot use v (type interface {}) as type string in field value: need type assertion
最佳答案
已解决。回答:首先,我的结构 VALUE 是 string
类型而不是 interaface
,其次 - 为了查看数据解析,我必须实际编码它......使用: x, _ := xml.MarshalIndent( map (数据), "", "")
关于go - 如何从 map[string]interface{} 编码 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54201711/
我是一名优秀的程序员,十分优秀!