在Go语言中将JSON转换为XML。

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

JSON to XML in go lang

问题

package main

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

type Address struct {
Address1 string json:"Address1"
Address2 string json:"Address2"
City string json:"city"
State string json:"state"
Zipcode string json:"zipcode"
}

func main() {
jsonData := {"Address1": "777 Brockton Avenue", "Address2": "string", "city": "Abington", "state": "MA", "zipcode": "2351"}
var person Address
err := json.Unmarshal([]byte(jsonData), &person)
if err != nil {
panic(err)
}
xmlData, err := xml.MarshalIndent(person, "", " ")
if err != nil {
panic(err)
}
fmt.Println(xml.Header + string(xmlData))
}

期望的输出是:

<?xml version="1.0"?>
<Address>
  <Address1>777 Brockton Avenue</Address1>
  <Address2>string</Address2>
  <City>Abington</City>
  <State>MA</State>
  <Zip5>2351</Zip5>
  <Zip4></Zip4>
</Address>
英文:

Hi I am trying the below code in go lang, where i have to change a json data to xml and pass it to another service. Below is the function that i tried for json to xml conversion

package main

import (
    &quot;encoding/json&quot;
    &quot;encoding/xml&quot;
    &quot;fmt&quot;
)

type Address struct {
        Address1 string `json:&quot;Address1&quot;`
        Address2  string    `json:&quot;Address2&quot;`
        city  string    `json:&quot;city&quot;`
        state  string    `json:&quot;state&quot;`
        zipcode  string    `json:&quot;zipcode&quot;`
}

func main() {
    jsonData := `{  &quot;Address1&quot;: &quot;777 Brockton Avenue&quot;,
  &quot;Address2&quot;: &quot;string&quot;,
  &quot;city&quot;: &quot;Abington&quot;,
  &quot;state&quot;: &quot;MA&quot;,
  &quot;zipcode&quot;: &quot;2351&quot;}`
    var person Address
    err := json.Unmarshal([]byte(jsonData), &amp;person)
    if err != nil {
        panic(err)
    }
    xmlData, err := xml.Marshal(person)
    if err != nil {
        panic(err)
    }
    fmt.Println(string(xmlData))
}

The output is as below

&lt;Address&gt;&lt;Address1&gt;777 Brockton Avenue&lt;/Address1&gt;&lt;Address2&gt;string&lt;/Address2&gt;&lt;/Address&gt;

My expected output is

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;AddressValidateRequest USERID=&quot;702DIGIT27hfgjkf41&quot;&gt;
  &lt;Address&gt;
    &lt;Address1&gt;777 Brockton Avenue&lt;/Address1&gt;
    &lt;Address2&gt;string&lt;/Address2&gt;
    &lt;City&gt;Abington&lt;/City&gt;
    &lt;State&gt;MA&lt;/State&gt;
    &lt;Zip5&gt;2351&lt;/Zip5&gt;
    &lt;Zip4/&gt;
  &lt;/Address&gt;
&lt;/AddressValidateRequest&gt;

I am quite new to the language,any guidance is much appreciated

答案1

得分: 1

创建另一个名为AddressValidateRequest的结构体,包含NameUserIDAddress字段。由于该结构体是根据你期望的输出定义的,所以首字母应该大写。

同时,CityStateZipcode字段的首字母也应该大写,否则它们将不会包含在XML输出中。

以下是代码:

package main

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

type Address struct {
	Address1 string `json:"Address1"`
	Address2 string `json:"Address2"`
	City     string `json:"City"`
	State    string `json:"State"`
	Zip5     string `json:"Zipcode"`
	Zip4     string `json:"Zip4"`
}

type AddressValidateRequest struct {
	Name    string  `xml:"AddressValidateRequest"`
	UserID  string  `xml:"USERID,attr"`
	Address Address `xml:"Address"`
}

func main() {
	jsonData := `{
		"Address1": "777 Brockton Avenue",
		"Address2": "string",
		"City": "Abington",
		"State": "MA",
		"Zipcode": "2351"
	}`

	var person Address
	err := json.Unmarshal([]byte(jsonData), &person)
	if err != nil {
		panic(err)
	}

	req := AddressValidateRequest{
		UserID:  "702DIGIT2741",
		Address: person,
	}

	xmlData, err := xml.MarshalIndent(req, "", "  ")
	if err != nil {
		panic(err)
	}

	fmt.Printf("%s%s \n", xml.Header, string(xmlData))
}

以上是翻译好的代码部分。

英文:

Create another struct as AddressValidateRequest with Name, UserID and Address fields. As this structure is defined based on the output you expected.

Also first character should be in uppercase letter for the City, State and Zipcode fields. If not they'll not included in the xml output.

Here is the code

package main

import (
	&quot;encoding/json&quot;
	&quot;encoding/xml&quot;
	&quot;fmt&quot;
)

type Address struct {
	Address1 string `json:&quot;Address1&quot;`
	Address2 string `json:&quot;Address2&quot;`
	City     string `json:&quot;city&quot;`
	State    string `json:&quot;state&quot;`
	Zip5     string `json:&quot;zipcode&quot;`
	Zip4     string `json:&quot;Zip4&quot;`
}

type AddressValidateRequest struct {
	Name string  `xml:&quot;AddressValidateRequest&quot;`
	UserID  string  `xml:&quot;USERID,attr&quot;`
	Address Address `xml:&quot;Address&quot;`
}

func main() {
	jsonData := `{  
		&quot;Address1&quot;: &quot;777 Brockton Avenue&quot;,
  		&quot;Address2&quot;: &quot;string&quot;,
  		&quot;city&quot;: &quot;Abington&quot;,
  		&quot;state&quot;: &quot;MA&quot;,
  		&quot;zipcode&quot;: &quot;2351&quot;
	}`

	var person Address
	err := json.Unmarshal([]byte(jsonData), &amp;person)
	if err != nil {
		panic(err)
	}

	req := AddressValidateRequest{
		UserID:  &quot;702DIGIT2741&quot;,
		Address: person,
	}

	xmlData, err := xml.MarshalIndent(req, &quot;&quot;, &quot; &quot;)
	if err != nil {
		panic(err)
	}

	fmt.Printf(&quot;%s%s \n&quot;, xml.Header, string(xmlData))

}

huangapple
  • 本文由 发表于 2023年7月11日 19:25:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76661477.html
匿名

发表评论

匿名网友

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

确定