I am using this code to serialize the users into json
text file.
if (File.Exists(path))
{
using (var file = File.CreateText(path))
{
var serializer = new JsonSerializer();
serializer.Serialize(file, this.users);
}
}
This is the result that I get :
[
How can I get a result like this :
[
NET objects as JSON (serialize) To write JSON to a string or to a file, call the JsonSerializer. Serialize method. The JSON output is minified (whitespace, indentation, and new-line characters are removed) by default.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
Json.NET has excellent support for serializing and deserializing collections of objects. To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for.
Use json. dumps() to serialize a list into a JSON object. Use json. dumps(list) to serialize list into a JSON string.
Set the formatting on the serializer to Indented.
var serializer = new JsonSerializer();
serializer.Formatting = Formatting.Indented;
serializer.Serialize(file, this.users);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With