为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Golang 中将 []byte XML 转换为 JSON 输出

如何在 Golang 中将 []byte XML 转换为 JSON 输出

慕姐8265434 2021-08-16 19:01:13
有没有办法在 Golang 中将 XML ([]byte) 转换为 JSON 输出?我有下面的函数 where bodyis[]byte但我想在一些操作后将此 XML 响应转换为 JSON。我试着Unmarshal在xml包没有成功:// POST func (u *UserResource) authenticateUser(request *restful.Request, response *restful.Response) {    App := new(Api)    App.url = "http://api.com/api"    usr := new(User)    err := request.ReadEntity(usr)    if err != nil {        response.AddHeader("Content-Type", "application/json")        response.WriteErrorString(http.StatusInternalServerError, err.Error())        return    }    buf := []byte("<app version=\"1.0\"><request>1111</request></app>")    r, err := http.Post(App.url, "text/plain", bytes.NewBuffer(buf))    if err != nil {        response.AddHeader("Content-Type", "application/json")        response.WriteErrorString(http.StatusInternalServerError, err.Error())        return    }    defer r.Body.Close()    body, err := ioutil.ReadAll(r.Body)    response.AddHeader("Content-Type", "application/json")    response.WriteHeader(http.StatusCreated)//  err = xml.Unmarshal(body, &usr)//  if err != nil {//      fmt.Printf("error: %v", err)//      return//  }    response.Write(body)//  fmt.Print(&usr.userName)}我也在使用 Go-restful 包
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 304 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信