【问题标题】:howto Serialize a Nested collection using JsonWriter in C#如何在 C# 中使用 JsonWriter 序列化嵌套集合
【发布时间】:2016-06-07 04:13:19
【问题描述】:

我需要生成与使用 JSON 在 XML 中生成的输出相同的输出

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

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

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

我已经能够制作这个了:

{
  "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 序列化器如何序列化我的目标类的示例:

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

该类是 MyClass,包含一个 Item 类型的 Classes 集合。

【问题讨论】:

  • 您能否编辑您的问题以包含您想要创建的 JSON 样本?

标签: c# json excel serialization


【解决方案1】:

你应该只写一次属性名"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" 字符串。)

【讨论】:

  • 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
  • 1970-01-01
  • 2014-07-02
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多