我有以下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>
标记。
答案 0 :(得分:2)
这是因为您有额外的[
和]
以下
"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>