Golang学习笔记--xml字符串转json

Go语言中struct和xml,struct和json之间的互相转换比较容易实现,那么xml直接转json

该如何实现?其本质还是通过struct间接转换的。让我们直接看下代码:

package main

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"os"
)

var xmlDataRq = `


     880061
     0000
     B00000
     20110224
     14:49:20
     20110224
     12345678
     1
     ok


     
            1204300001
            e10adc3949ba59abbe56e057f20f883e
            127.0.0.1
     
     
            1204300002
            e10adc3949ba59abbe56e057f20f883e2
            127.0.0.2
     



`

type head struct {
	H_exch_code string `xml:"h_exch_code" json:"h_exch_code"`
	H_bank_no   string `xml:"h_bank_no" json:"h_bank_no"`
	H_branch_id string `xml:"h_branch_id" json:"h_branch_id"`
	H_fact_date string `xml:"h_fact_date" json:"h_fact_date"`
	H_fact_time string `xml:"h_fact_time" json:"h_fact_time"`
	H_exch_date string `xml:"h_exch_date" json:"h_exch_date"`
	H_serial_no string `xml:"h_serial_no" json:"h_serial_no"`
	H_rsp_code  string `xml:"h_rsp_code" json:"h_rsp_code"`
	H_rsp_msg   string `xml:"h_rsp_msg" json:"h_rsp_msg"`
}

type rqRecord struct {
	User_id  string `xml:"user_id" json:"user_id"`
	User_pwd string `xml:"user_pwd" json:"user_pwd"`
	Login_ip string `xml:"login_ip" json:"login_ip"`
}

type rqBody struct {
	Record []rqRecord `xml:"record" json:"record"`
}

type rspRecord struct {
	Server_code         string `xml:"server_code" json:"server_code"`
	Server_name         string `xml:"server_name" json:"server_name"`
	Trans_ip            string `xml:"trans_ip" json:"trans_ip"`
	Trans_port          string `xml:"trans_port" json:"trans_port"`
	Query_ip            string `xml:"query_ip" json:"query_ip"`
	Query_port          string `xml:"query_port" json:"query_port"`
	Broadcast_ip        string `xml:"broadcast_ip" json:"broadcast_ip"`
	Broadcast_port      string `xml:"broadcast_port" json:"broadcast_port"`
	Risk_trans_ip       string `xml:"risk_trans_ip" json:"risk_trans_ip"`
	Risk_trans_port     string `xml:"risk_trans_port" json:"risk_trans_port"`
	Risk_broadcast_ip   string `xml:"risk_broadcast_ip" json:"risk_broadcast_ip"`
	Risk_broadcast_port string `xml:"risk_broadcast_port" json:"risk_broadcast_port"`
}

type rspBody struct {
	Record []rspRecord `xml:"record" json:"record"`
}

type Request struct {
	XMLName xml.Name `xml:"request" json:"request"`
	Head    head     `xml:"head" json:"head"`
	Body    rqBody   `xml:"body" json:"body"`
}

type Response struct {
	XMLName xml.Name `xml:"response" json:"response"`
	Head    head     `xml:"head" json:"head"`
	Body    rspBody  `xml:"body" json:"body"`
}

var xmlDataRsp = `


     880061
     0000
     B00000
     20110224
     14:49:20
     20110224
     12345678
     HJ0000
     处理成功


     
            M01
            内网 
            168.33.114.248
            15000
            168.33.114.248
            16000
            168.33.114.248
            17000
            168.33.114.248
            20000
            168.33.114.248
            21000
     
     
            M02
            外网
            210.21.197.124
            15000
            210.21.197.124
            16000
            210.21.197.124
            17000
            210.21.197.124
            20000
            210.21.197.124
            21000
     

`

func main() {
	v := Request{}
	xml.Unmarshal([]byte(xmlDataRq), &v)

	// sanity check - XML level
	fmt.Printf("XMLName: %#v\n", v.XMLName)
	fmt.Printf("XMLName: %#v\n", v.Head)
	fmt.Printf("Body: %#v\n", v.Body)

	fmt.Println("---json")
	b, _ := json.Marshal(v)
	os.Stdout.Write(b)

	fmt.Println("------")

	v2 := Response{}
	xml.Unmarshal([]byte(xmlDataRsp), &v2)

	// sanity check - XML level
	fmt.Printf("XMLName: %#v\n", v2.XMLName)
	fmt.Printf("XMLName: %#v\n", v2.Head)
	fmt.Printf("Body: %#v\n", v2.Body)

	fmt.Println("---json")
	b2, _ := json.Marshal(v2)
	os.Stdout.Write(b2)
}

 

你可能感兴趣的:(Golang,golang学习)