为什么DeserializeXmlNode输出XML将数组元素包含在额外标记中?

时间:2015-06-19 06:50:25

标签: json xml json.net deserialization

我有以下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>标记。

1 个答案:

答案 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>

工作小提琴:https://dotnetfiddle.net/Oyj91s