将XML反序列化为JSON数组时出现C#问题

sigwle7e  于 2023-03-13  发布在  C#
关注(0)|答案(3)|浏览(202)

我有以下XML

<table>
    <name>Table 1</name>
    <headers>
        <header>Header 1</header>
        <header>Header 2</header>
    </headers>
    <data>
        <row>
            <row_item>Item 1</row_item>
            <row_item>Item 2</row_item>
        </row>
        <row>
            <row_item>Item 3</row_item>
            <row_item>Item 4</row_item>
        </row>
    </data>
</table>

现在,我可以使用内置的XmlSerializer类对此类进行反序列化。

[JsonObject("table")]
[XmlRoot("table")]
public class Table
{
    [JsonProperty("name")]
    [XmlElement("name")]
    public string Name { get; set; }

    [JsonProperty("headers")]
    [XmlArray("headers"), XmlArrayItem("header")]
    public List<string> Headers { get; set; } = new List<string>();

    [JsonProperty("data")]
    [XmlArray("data"), XmlArrayItem("row")]
    public List<DataRow> Data { get; set; } = new List<DataRow>();

}

public class DataRow
{
    [XmlElement("row_item")]
    public List<string> DataRowItems { get; set; }
}

现在我需要将其转换为JSON,我使用Newtonsoft来完成此操作,但我面临的问题是当前的类结构将“DataRowItems”添加到JSON。

{
    "table": [
        {
            "data": [
                {
                    "DataRowItems": [
                        "Item 1",
                        "Item 2"
                    ]
                },
                {
                    "DataRowItems": [
                        "Item 3",
                        "Item 4"
                    ]
                }
            ],
            "headers": [
                "Item 1",
                "Item 2",
                "Item 3"
            ],
            "name": "Some text"
        }
    ]
}

因为我需要把data元素作为数组的数组发送,就像这样。

{
  "table": [
    {
      "data": [
        ["Item 1", "Item 2", "Item 3"],
        ["Item 4", "Item 5", "Item 6"]
      ],
      "headers": ["Column 1", "Column 2", "Column 3"],
      "name": "Some text"
    }
  ]
}

我怎样才能做到这一点呢?我已经尝试了各种XML和JSON属性,但似乎无法让它工作。我也尝试了使用List而不是List来正确创建JSON,但XML无法去序列化。

mnemlml8

mnemlml81#

删除DataRow并按如下方式放置嵌套列表:

[JsonObject("table")]
    [XmlRoot("table")]
    public class Table
    {
        [JsonProperty("name")]
        [XmlElement("name")]
        public string Name { get; set; }

        [JsonProperty("headers")]
        [XmlArray("headers"), XmlArrayItem("header")]
        public List<string> Headers { get; set; } = new List<string>();

        [JsonProperty("data")]
        [XmlArray("data"), XmlArrayItem("row")]
        public List<List<string>> Data { get; set; } = new List<List<string>>();

    }

我获得了:{“名称”:“aaa”,“标头”:[“H1”,“H2”,“H3”],“数据”:[[“i1”,“i2”],[“i3”,“i4”]]}

5m1hhzi4

5m1hhzi42#

你可以试试这个,你不需要任何定制类

XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xml);
    
    var json = JsonConvert.SerializeXmlNode(xmlDoc, Newtonsoft.Json.Formatting.None, true);
    var jObj = JObject.Parse(json);

    jObj["headers"] = jObj["headers"]["header"];
    jObj["data"] = new JArray(jObj["data"]["row"].Select(x => x["row_item"]));
    json=jObj.ToString();

输出

{
  "name": "Table 1",
  "headers": [
    "Header 1",
    "Header 2"
  ],
  "data": [
    [
      "Item 1",
      "Item 2"
    ],
    [
      "Item 3",
      "Item 4"
    ]
  ]
}

或者如果你想要c#对象

DataClass data = jObj.ToObject<DataClass>();

public partial class DataClass
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("headers")]
    public List<string> Headers { get; set; }

    [JsonProperty("data")]
    public List<List<string>> Data { get; set; }
}
sy5wg1nm

sy5wg1nm3#

谢谢大家的建议,这是类中属性的最后设置,我用它来使它工作。

[JsonProperty("data")]
    [XmlArray("data")]
    [XmlArrayItem("row")]
    [XmlArrayItem("row_item", NestingLevel = 1)]
    public List<List<string>> Data { get; set; } = new List<List<string>>();

NestingLevel项是使row_item正确反序列化的关键。

相关问题