我有XML,我想将地址信息解组到其on数组中作为我的结构的一部分:
<customer>
...
<dob>1990-10-01</dob>
<address1>555 Hollywood Blvd</address1>
<city>Hollywood</city>
<state>CA</state>
<zipCode>99999</zipCode>
<alternateAddress1>575 Hollywood St</alternateAddress1>
<alternateCity>Los Angeles</alternateCity>
<alternateState>CA</alternateState>
<alternateZipCode>12345</alternateZipCode>
....
</customer>
我对结构的尝试:
type Test struct {
CustProfile struct {
DOB string `xml:"birthDate" json:"dob"`
Address []struct {
PrimaryAddress struct {
Street string `xml:"address1" json:"line1"`
City string `xml:"city" json:"city"`
State string `xml:"state" json:"state"`
ZipCode string `xml:"zipCode" json:"zip"`
IsPrimaryAddress string `json:"isPrimaryAddress"`
}
AlternateAddress struct {
Street string `xml:"alternateAddress1" json:"line1"`
City string `xml:"alternateCity" json:"city"`
State string `xml:"alternateState" json:"state"`
ZipCode string `xml:"alternateZipCode" json:"zip"`
IsPrimaryAddress string `json:"isPrimaryAddress"`
}
} `json:"address"`
} `xml:"customer" json:"custProfile"`
}
所以我在这里遇到了几个问题。第一个问题是所有元素都处于同一级别,并且从我到目前为止所知,因为CustProfile结构已经与客户父元素相关联,我无法引用Address结构中的所有子元素,因为路径将是 customer-&gt;?&gt; city,state等,它将始终返回null,因为该路径不存在。
第二个问题是我尝试为 IsPrimaryAddress 定义默认字符串的方式。我尝试过这样的事情,但我得到了一个未定义的错误。
var marshalTest Test
...
marshalTest.CustProfile.Address.PrimaryAddress.IsPrimaryAddress = "Y";
marshalTest.CustProfile.Address.AlternateAddress.IsPrimaryAddress = "N";
是否可以将此XML解组为导致下面结构的结构?
{
"custProfile": {
"dob": "1990-10-01",
"address": [
{
"line1": "555 Hollywood Blvd",
"city": "HOLLYWOOD",
"state": "CA",
"zip": "99999",
"isPrimaryAddress": "Y"
},
{
"line1": "575 Hollywood St",
"city": "LOS ANGELES",
"state": "CA",
"zip": "12345",
"isPrimaryAddress": "N"
}
]
}
我根本不熟悉Go中的XML编码,而且我能够管理的最好的是:
{
"custProfile": {
"dob": "1990-10-01",
"address": null
}
}
非常感谢任何帮助。感谢