gpt4 book ai didi

go - 如何从 map[string]interface{} 编码 XML?

转载 作者:数据小太阳 更新时间:2023-10-29 03:09:30 26 4
gpt4 key购买 nike

我正在构建一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com