简体   繁体   中英

Serializing into JSON using MemoryStream,while adding newlines C#

I have to serialize some C# class data into JSON. For this purpose I use a MemoryStream and a 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();
}

Running the application like this produces this output:

{"age":42,"arr":[{},{},{}],"name":"John","value":null,"w":{}}

However, for my task I have to produce a JSON file where each entry is entered in a new line. Example:

"SomeData":[
{
    "SomeData" : "Value",
    "SomeData" : 0,
    "SomeData": [
        {
            "SomeData" : "Value",
            "SomeData" : 0
            ]
        }
    ]
}, etc. etc.

Any ideas as to how I can do this? Thanks in advance!

Ok, so if you want to do that just add this code answered in this question.

question here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM