s

C# deserialize json to object edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 25 September 2020 | 2094

I have a javascript array of countries. I convert that to JSON and sent it to C#, Now I want to convert the JSON to a C# List. Below is the JSON I want to convert to C# list of objects.

[{"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"}]

I need to convert the list of countries into a C# list of countries like the class below.

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

Check the code to deserialize the object.

                

List<country> lstCountries = Newtonsoft.Json.JsonConvert.DeserializeObject<list<country>>(jsondata);