Convert List into Json Using C#.Net

How to serialize or convert object list to JSON string using C#.Net. I used JsonSerializer.Serialize(object) to serialize the object list to string.
In today's article I will show you a simple tutorial with an example to convert or serialize the given list into a Json format string by c#.net. JsonSerializer.Serialize(object) is used to serialize the list into a Json format. This example you can use in your asp.net core mvc as well as in your all ,net core applications. 

So, for this article first we will create a console application and add class file into it. In this class file we will add few properties of employee. 

 public class EmployeeModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
} 
In above class file i have added some properties for an employee. Now let's add some values to the list of employees. 
 List<EmployeeModel> employees = new List<EmployeeModel>() {
     new EmployeeModel { Id=1,Name="John" ,Address="Address1",Email="john@mail.com"},
     new EmployeeModel { Id=2,Name="Mate" ,Address="Address1",Email="mate@mail.com"},
     new EmployeeModel { Id=3,Name="Rose" ,Address="Address1",Email="rose@mail.com"},
     new EmployeeModel { Id=4,Name="Pape" ,Address="Address1",Email="pape@mail.com"}
};
var jsonValue = JsonSerializer.Serialize(employees);
Console.WriteLine(jsonValue);
Console.ReadLine(); 
In above code I have taken the list of employees and after that i have used  JsonSerializer.Serialize to convert the list object to Json string. Now we have done let's run the code top check the output. As ewe run the code break point will hit. 

List to Json in C#.net
Here you can see that the passed list got converted to Json string with the help of JsonSerializer.Serialize. Now let's press F5 to check the output.

Convert List into Json Using C#.Net

Post a Comment