如何在C#中使用JsonWriter序列化嵌套集合 [英] howto Serialize a Nested collection using JsonWriter in C#
问题描述
我需要产生与使用JSON在XML中产生的输出相同的输出
对于Excel工作表中的某些表格数据,数据的格式为:
Column1 Column2 Column3AAA bbb cccXXX YYY ZZZkkk jjj nnn
我需要使用Json Writer编写Json文件,因为我无法创建一个类来生成数据,但是我必须使用创建的JSON将其反序列化为另一个应用程序中的类.为了能够在消费类应用程序中反序列化该类,我需要一个可以将其命名为MyClass的类,该类包含一个Items集合,每个Item代表一个Row,标题,Column1,Column2,Column3是属性的名称./p>
我已经能够制作出这个:
{物品": {"Column1":"AAA","Column2":"BBB","Column3":"CCC",},物品": {"Column1":"XXX","Column2":"YYY","Column3":"ZZZ",},}
不幸的是,这不是包含Item集合的对象,因此它不会反序列化.
这是我用于从excel文件进行手动序列化的代码,我找不到的是如何对集合的开始和结束进行脚本编写:
StringBuilder sb = new StringBuilder();StringWriter sw =新的StringWriter(sb);JsonWriter jsonWriter = null;jsonWriter =新的JsonTextWriter(sw);jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;jsonWriter.WriteStartObject();int countAwait = 0;//在这里我想写什么来打开收藏集对于(int row = firstRow; row< = end.Row; row ++){数++;countAwait ++;如果(countAwait> = 10){ResultText =读取记录" +计数;countAwait = 0;}jsonWriter.WritePropertyName(RowElement);jsonWriter.WriteStartObject();对于(int col = start.Column; col< = end.Column; col ++){jsonWriter.WritePropertyName(fieldNames [col]);jsonWriter.WriteValue(GetCellStringValue(ws,row,col));}jsonWriter.WriteEndObject();}//在这里,我需要编写集合的结尾jsonWriter.WriteEndObject();jsonWriter.Close();
进行编辑以添加Json Serializer如何序列化我的目标类的示例:
{项目": [{"Column1":"XXX","Column2":"YYY","Column3":"ZZZ",},{"Column1":"AAA","Column2":"BBB","Column3":"CCC",}]}
该类为MyClass,并且包含Item类型的Classes的集合.
您应该只写一次属性名称"Items"
,然后使用 JsonWriter.WriteEndArray()
开始和结束JSON数组,然后将每一行写为嵌套在数组中的对象:
var sb = new StringBuilder();使用(var sw = new StringWriter(sb))使用(var jsonWriter = new JsonTextWriter(sw)){var countAwait = 0;jsonWriter.Formatting = Newtonsoft.Json.Formatting.Indented;jsonWriter.WriteStartObject();//写入根对象的开头jsonWriter.WritePropertyName(RowElement);//写入"Items"属性名称jsonWriter.WriteStartArray();//写出"Items"数组的开头for(int row = firstRow; row< = end.Row; row ++){数++;countAwait ++;如果(countAwait> = 10){ResultText =读取记录" +计数;countAwait = 0;}jsonWriter.WriteStartObject();//在"Items"数组中写入条目的开头对于(int col = start.Column; col< = end.Column; col ++){jsonWriter.WritePropertyName(fieldNames [col]);jsonWriter.WriteValue(GetCellStringValue(ws,row,col));}jsonWriter.WriteEndObject();//将条目的结尾写入"Items"数组中}jsonWriter.WriteEndArray();//编写"Items"数组的结尾.jsonWriter.WriteEndObject();//写入根对象的结束符//在using语句中时,无需显式关闭}
(这里我假设 RowElement
与"Items"
字符串相对应.)
I need to produce the same output I produce in XML using JSON
For some tabular data that is in an Excel Worksheet, data is in the format:
Column1 Column2 Column3
AAA bbb ccc
XXX YYY ZZZ
kkk jjj nnn
I need to write the Json file Using the Json Writer because I can't create a class to produce the data, but I have then to use the created JSON deserializing it as a class in a different application. To be able to deserialize the class in the consumer application I need to have a class we can name it MyClass containing a collection of Items, each Item representing a Row, the headers, Column1, Column2, Column3 are the names of the properties.
I've been able to produce this:
{
"Item": {
"Column1": "AAA",
"Column2": "BBB",
"Column3": "CCC",
},
"Item": {
"Column1": "XXX",
"Column2": "YYY",
"Column3": "ZZZ",
},
}
Unfortunately this is not an object containing a collection of Item so It does not deserialize.
This is the code I use for the manual serialization from the excel file, what I was not able to find is how to script the start and end of the collection:
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();
Edited to add how the Json Serializer serializes a sample of my destination class:
{
"Items": [
{
"Column1": "XXX",
"Column2": "YYY",
"Column3": "ZZZ",
},
{
"Column1": "AAA",
"Column2": "BBB",
"Column3": "CCC",
}
]
}
The class is MyClass and Contains a collection Items of Classes of type Item.
You should write the property name "Items"
only once, then for its value use JsonWriter.WriteStartArray()
and JsonWriter.WriteEndArray()
to start and end a JSON array, then write each row as an object nested in the array:
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
}
(Here I am assuming that RowElement
corresponds to the "Items"
string.)
这篇关于如何在C#中使用JsonWriter序列化嵌套集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!