英文:
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 (
"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.Marshal(person)
if err != nil {
panic(err)
}
fmt.Println(string(xmlData))
}
The output is as below
<Address><Address1>777 Brockton Avenue</Address1><Address2>string</Address2></Address>
My expected output is
<?xml version="1.0"?>
<AddressValidateRequest USERID="702DIGIT27hfgjkf41">
<Address>
<Address1>777 Brockton Avenue</Address1>
<Address2>string</Address2>
<City>Abington</City>
<State>MA</State>
<Zip5>2351</Zip5>
<Zip4/>
</Address>
</AddressValidateRequest>
I am quite new to the language,any guidance is much appreciated
答案1
得分: 1
创建另一个名为AddressValidateRequest
的结构体,包含Name
、UserID
和Address
字段。由于该结构体是根据你期望的输出定义的,所以首字母应该大写。
同时,City
、State
和Zipcode
字段的首字母也应该大写,否则它们将不会包含在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 (
"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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论