To serialize a class to JSON in C#, we can use the Newtonsoft.Json NuGet package.
Here's an example:
main.cs325 chars15 lines
In this example, we define a Person
class with two properties (Name
and Age
), and create an instance of the class with some sample data. We then use the JsonConvert.SerializeObject
method from the Newtonsoft.Json package to serialize the class to JSON, and store the resulting JSON string in a variable named json
.
gistlibby LogSnag