如何在Golang中将[] byte XML转换为JSON输出 [英] How to convert []byte XML to JSON output in Golang

查看:182
本文介绍了如何在Golang中将[] byte XML转换为JSON输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有下面的函数,其中 body [] byte ,但是我想在经过一些处理之后将这个XML响应转换为JSON。我在 xml 包中尝试了 Unmarshal ,但没有成功:

  // POST 
func(u * UserResource)authenticateUser(request * restful.Request,response * restful.Response){
App:= new )
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()
身体, 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软件包

解决方案

如果您需要将XML文档转换为带有未知结构的JSON,则可以使用 goxml2json

  import(
//其他进口。 ..
xjgithub.com/basgys/goxml2json


func(u * UserResource)authenticateUser(request * restful.Request,response * restful.Response){
//从restful.Request提取数据
xml:= strings.NewReader(`<?xml version =1 .0encoding =UTF-8?>< app version =1.0>< request> 1111< / request>< / app>`)

//转换
json,err:= xj.Convert(xml)
if err!= nil {
//糟糕...
}

// ...使用JSON ...
}




注意:我是这个图书馆的作者。



Is there a way to convert XML ([]byte) to JSON output in Golang?

I've got below function where body is []byte but I want to transform this XML response to JSON after some manipulation. I've tried Unmarshal in xml package with no success:

// 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)
}

I'm also using Go-restful package

解决方案

If you need to convert an XML document to JSON with an unknown struct, you can use goxml2json.

Example :

import (
  // Other imports ...
  xj "github.com/basgys/goxml2json"
)

func (u *UserResource) authenticateUser(request *restful.Request, response *restful.Response) {
  // Extract data from restful.Request
  xml := strings.NewReader(`<?xml version="1.0" encoding="UTF-8"?><app version="1.0"><request>1111</request></app>`)

  // Convert
    json, err := xj.Convert(xml)
    if err != nil {
        // Oops...
    }

  // ... Use JSON ...
}

Note : I'm the author of this library.

这篇关于如何在Golang中将[] byte XML转换为JSON输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆