public class Person { public string Name { get; set; } public int Age { get; set; } } Person person = new() { Name = "John", Age = 30 }; string json = JsonSerializer.Serialize(person); Console.WriteLine(json);
string json = "{\"Name\":\"John\",\"Age\":30}"; Person person = JsonSerializer.DeserializeIn this example, we have a JSON string and use the JsonSerializer.Deserialize method to convert it into a Person object. The output will be "John" as we access the Name property of the Person object. Package Library: The `System.Text.Json` namespace is included in the .NET Core runtime, so no additional packages are required to use IJsonSerializer. However, if you're using an older version of .NET, you may need to add the `System.Text.Json` NuGet package to your project.(json); Console.WriteLine(person.Name); // Output: John