我必须将一些C#类数据序列化为JSON。为此,我使用了MemoryStream和DataContractJsonSerializer。
MemoryStream stream1 = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Person));
ser.WriteObject(stream1, p);
using (FileStream file = new FileStream("Write.json", FileMode.Create, FileAccess.ReadWrite))
{
stream1.Position = 0;
stream1.Read(stream1.ToArray(), 0, (int)stream1.Length);
file.Write(stream1.ToArray(), 0, stream1.ToArray().Length);
stream1.Close();
file.Close();
}
像这样运行应用程序会产生以下输出:
{"age":42,"arr":[{},{},{}],"name":"John","value":null,"w":{}}
但是,对于我的任务,我必须生成一个JSON文件,其中每个条目都以新行输入。例如:
"SomeData":[
{
"SomeData" : "Value",
"SomeData" : 0,
"SomeData": [
{
"SomeData" : "Value",
"SomeData" : 0
]
}
]
}, etc. etc.
关于我如何做到这一点的任何想法?提前谢谢!