.net core serialize object to json edit button Edit

Murugan Andezuthu Dharmaratnam | calendar 17 September 2020 | 2473

How do I serialize a list of objects to json in .Net code. I have a list of countries with properties Id, Code & Name. I want to serialize the list to json.

public class Country
    public int Id { get; set; }
    public int Code { get; set; }
    public String Name { get; set; }

If using .Net Core 3.0 or later you can use the default built in System.Text.Json parser

using System.Text.Json;

List<country> lstCountries = new Country().Get();
String countryJson = JsonSerializer.Serialize(lstCountries);

Here is the serialized JSON code.