How to convert Json string to xml in Golang?

huangapple go评论74阅读模式
英文:

How to convert Json string to xml in Golang?

问题

我正在尝试使用Golang将JSON字符串转换为XML格式。我的目标是将JSON转换为map[string]interface{},然后将接口转换为XML文件(没有预定义的结构体)。请帮助我解决这个问题。

var f interface{}
err := json.Unmarshal(b, &f)

我会将代码部分翻译为中文,其他部分不做翻译。

英文:

I'm trying to convert json string in to xml format using Golang. My whole object is to convert json in to map string interface and then convert interface in to xml file. (There is no predefined structs). Please help me to solve this issue?

var f interface{}
err := json.Unmarshal(b, &f)

答案1

得分: 3

有一个与JSON包在编码方面等效的XML包。只需导入它,然后对json.Unmarshal的结果进行编组。

import "encoding/xml"
xml.Marshal(&f)
英文:

There's an xml package that is equivalent to the json one under encoding. Just import it and then marshal the result of your json.Unmarshal

import "encoding/xml"
xml.Marshal(&f)

huangapple
  • 本文由 发表于 2015年9月9日 01:55:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/32464262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定