如何在C#中使用JsonWriter序列化嵌套集合

时间:2016-06-06 19:17:50

标签: c# json excel serialization

我需要使用JSON

生成我在XML中生成的相同输出

对于Excel工作表中的某些表格数据,数据格式为:

Column1 Column2 Column3
AAA      bbb     ccc
XXX      YYY     ZZZ
kkk      jjj     nnn

我需要编写Json文件使用Json Writer,因为我无法创建一个类来生成数据,但我必须使用创建的JSON将其反序列化为另一个应用程序中的类。 为了能够在消费者应用程序中反序列化类,我需要有一个类,我们可以将它命名为包含Items集合的MyClass,每个Item表示一行,header,Column1,Column2,Column3是属性的名称。 / p>

我已经能够产生这个:

{
  "Item": {
    "Column1": "AAA",
    "Column2": "BBB",
    "Column3": "CCC",
  },
  "Item": {
    "Column1": "XXX",
    "Column2": "YYY",
    "Column3": "ZZZ",
  },
 }

不幸的是,这不是包含Item集合的对象,因此它不会反序列化。

这是我用于从excel文件手动序列化的代码,我无法找到的是如何编写集合的开头和结尾的脚本:

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
JsonWriter jsonWriter = null;
jsonWriter = new JsonTextWriter(sw);


jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
jsonWriter.WriteStartObject();
int countAwait = 0;
// Here I miss what to write to open the collection
for (int row = firstRow; row <= end.Row; row++)
{
    count++;
    countAwait++;
    if (countAwait >= 10)
    {
        ResultText = "Reading record " + count;
        countAwait = 0;
    }
    jsonWriter.WritePropertyName(RowElement);
    jsonWriter.WriteStartObject();
    for (int col = start.Column; col <= end.Column; col++)
    {
        jsonWriter.WritePropertyName(fieldNames[col]);
        jsonWriter.WriteValue(GetCellStringValue(ws, row, col));

    }
    jsonWriter.WriteEndObject();
}
// Here I need to write the closing of the collection

jsonWriter.WriteEndObject();
jsonWriter.Close();

编辑以添加Json Serializer如何序列化目标类的样本:

{
  "Items": [
    {
      "Column1": "XXX",
      "Column2": "YYY",
      "Column3": "ZZZ",
    },
    {
      "Column1": "AAA",
      "Column2": "BBB",
      "Column3": "CCC",
    }
  ]
}

该类是MyClass,包含Item类型的集合项。

1 个答案:

答案 0 :(得分:2)

您应该只编写一次属性名称"Items",然后将其值JsonWriter.WriteStartArray()JsonWriter.WriteEndArray()用于开始和结束JSON数组,然后将每一行写为嵌套的对象数组:

var sb = new StringBuilder();
using (var sw = new StringWriter(sb))
using (var jsonWriter = new JsonTextWriter(sw))
{
    var countAwait = 0;

    jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;
    jsonWriter.WriteStartObject(); // Write the opening of the root object
    jsonWriter.WritePropertyName(RowElement); // Write the "Items" property name
    jsonWriter.WriteStartArray(); // Write the opening of the "Items" array

    for (int row = firstRow; row <= end.Row; row++)
    {
        count++;
        countAwait++;
        if (countAwait >= 10)
        {
            ResultText = "Reading record " + count;
            countAwait = 0;
        }
        jsonWriter.WriteStartObject(); // Write the beginning of an entry in the "Items" array
        for (int col = start.Column; col <= end.Column; col++)
        {
            jsonWriter.WritePropertyName(fieldNames[col]);
            jsonWriter.WriteValue(GetCellStringValue(ws, row, col));

        }
        jsonWriter.WriteEndObject(); // Write the ending of an entry in the "Items" array
    }

    jsonWriter.WriteEndArray(); // Write the closing of the "Items" array.
    jsonWriter.WriteEndObject(); // Write the closing of the root object
    // No need to close explicitly when inside a using statement
}

(这里我假设RowElement对应于"Items"字符串。)