.net core serialize object to json Edit
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.
[{"Id":1,"Code":91,"Name":"India"},{"Id":2,"Code":86,"Name":"China"},{"Id":3,"Code":1,"Name":"USA"},{"Id":4,"Code":91,"Name":"India"},{"Id":5,"Code":7,"Name":"Russia"},{"Id":6,"Code":91,"Name":"India"},{"Id":7,"Code":86,"Name":"China"}]