为什么 DeserializeXmlNode 输出 XML 将数组元素包含在额外的标记中?
Why does the DeserializeXmlNode output XML enclose array elements in an extra tag?
我有以下 JSON 我正在转换为 XML:
{
"root": {
"object": [{
"Name": "Cust1",
"addresses": [
[{
"AddressLine1": "Address1",
"AddressLine2": "Address2"
}],
[{
"AddressLine1": "Address3",
"AddressLine2": "Address4"
}]
]
}, {
"Name": "Cust2",
"addresses": [
[{
"AddressLine1": "Address1_2",
"AddressLine2": "Address2_2"
}],
[{
"AddressLine1": "Address3_2",
"AddressLine2": "Address4_2"
}]
]
}]
}
}
我用下面的代码把它转换成XML(根元素是在JSON转换后手动添加的):
XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(rootJson);
输出XML如下:
<root>
<object>
<Name>Cust1</Name>
<addresses>
<addresses>
<AddressLine1>Address1</AddressLine1>
<AddressLine2>Address2</AddressLine2>
</addresses>
</addresses>
<addresses>
<addresses>
<AddressLine1>Address3</AddressLine1>
<AddressLine2>Address4</AddressLine2>
</addresses>
</addresses>
</object>
<object>
<Name>Cust2</Name>
<addresses>
<addresses>
<AddressLine1>Address1_2</AddressLine1>
<AddressLine2>Address2_2</AddressLine2>
</addresses>
</addresses>
<addresses>
<addresses>
<AddressLine1>Address3_2</AddressLine1>
<AddressLine2>Address4_2</AddressLine2>
</addresses>
</addresses>
</object>
问题出在 <addresses>
标签后面跟着另一个 <addresses>
标签。
因为你下面多了一个[
和]
"addresses": [
[{
"AddressLine1": "Address1",
"AddressLine2": "Address2"
}],
[{
"AddressLine1": "Address3",
"AddressLine2": "Address4"
}]
]
如果像下面那样删除多余的 [
和 ]
"addresses": [
{
"AddressLine1": "Address1",
"AddressLine2": "Address2"
},
{
"AddressLine1": "Address3",
"AddressLine2": "Address4"
}
]
你的json会是这样
{
"root": {
"object": [{
"Name": "Cust1",
"addresses": [
{
"AddressLine1": "Address1",
"AddressLine2": "Address2"
},
{
"AddressLine1": "Address3",
"AddressLine2": "Address4"
}
]
}, {
"Name": "Cust2",
"addresses": [
{
"AddressLine1": "Address1_2",
"AddressLine2": "Address2_2"
},
{
"AddressLine1": "Address3_2",
"AddressLine2": "Address4_2"
}
]
}]
}
}
您将得到以下输出 XML
<root>
<object>
<Name>Cust1</Name>
<addresses>
<AddressLine1>Address1</AddressLine1>
<AddressLine2>Address2</AddressLine2>
</addresses>
<addresses>
<AddressLine1>Address3</AddressLine1>
<AddressLine2>Address4</AddressLine2>
</addresses>
</object>
<object>
<Name>Cust2</Name>
<addresses>
<AddressLine1>Address1_2</AddressLine1>
<AddressLine2>Address2_2</AddressLine2>
</addresses>
<addresses>
<AddressLine1>Address3_2</AddressLine1>
<AddressLine2>Address4_2</AddressLine2>
</addresses>
</object>
</root>
工作fiddle:https://dotnetfiddle.net/Oyj91s
我有以下 JSON 我正在转换为 XML:
{
"root": {
"object": [{
"Name": "Cust1",
"addresses": [
[{
"AddressLine1": "Address1",
"AddressLine2": "Address2"
}],
[{
"AddressLine1": "Address3",
"AddressLine2": "Address4"
}]
]
}, {
"Name": "Cust2",
"addresses": [
[{
"AddressLine1": "Address1_2",
"AddressLine2": "Address2_2"
}],
[{
"AddressLine1": "Address3_2",
"AddressLine2": "Address4_2"
}]
]
}]
}
}
我用下面的代码把它转换成XML(根元素是在JSON转换后手动添加的):
XmlDocument xmlDoc = JsonConvert.DeserializeXmlNode(rootJson);
输出XML如下:
<root>
<object>
<Name>Cust1</Name>
<addresses>
<addresses>
<AddressLine1>Address1</AddressLine1>
<AddressLine2>Address2</AddressLine2>
</addresses>
</addresses>
<addresses>
<addresses>
<AddressLine1>Address3</AddressLine1>
<AddressLine2>Address4</AddressLine2>
</addresses>
</addresses>
</object>
<object>
<Name>Cust2</Name>
<addresses>
<addresses>
<AddressLine1>Address1_2</AddressLine1>
<AddressLine2>Address2_2</AddressLine2>
</addresses>
</addresses>
<addresses>
<addresses>
<AddressLine1>Address3_2</AddressLine1>
<AddressLine2>Address4_2</AddressLine2>
</addresses>
</addresses>
</object>
问题出在 <addresses>
标签后面跟着另一个 <addresses>
标签。
因为你下面多了一个[
和]
"addresses": [
[{
"AddressLine1": "Address1",
"AddressLine2": "Address2"
}],
[{
"AddressLine1": "Address3",
"AddressLine2": "Address4"
}]
]
如果像下面那样删除多余的 [
和 ]
"addresses": [
{
"AddressLine1": "Address1",
"AddressLine2": "Address2"
},
{
"AddressLine1": "Address3",
"AddressLine2": "Address4"
}
]
你的json会是这样
{
"root": {
"object": [{
"Name": "Cust1",
"addresses": [
{
"AddressLine1": "Address1",
"AddressLine2": "Address2"
},
{
"AddressLine1": "Address3",
"AddressLine2": "Address4"
}
]
}, {
"Name": "Cust2",
"addresses": [
{
"AddressLine1": "Address1_2",
"AddressLine2": "Address2_2"
},
{
"AddressLine1": "Address3_2",
"AddressLine2": "Address4_2"
}
]
}]
}
}
您将得到以下输出 XML
<root>
<object>
<Name>Cust1</Name>
<addresses>
<AddressLine1>Address1</AddressLine1>
<AddressLine2>Address2</AddressLine2>
</addresses>
<addresses>
<AddressLine1>Address3</AddressLine1>
<AddressLine2>Address4</AddressLine2>
</addresses>
</object>
<object>
<Name>Cust2</Name>
<addresses>
<AddressLine1>Address1_2</AddressLine1>
<AddressLine2>Address2_2</AddressLine2>
</addresses>
<addresses>
<AddressLine1>Address3_2</AddressLine1>
<AddressLine2>Address4_2</AddressLine2>
</addresses>
</object>
</root>
工作fiddle:https://dotnetfiddle.net/Oyj91s