Convert Json to Custom Collection C# code example

Convert Json to Custom Collection C# code example

Json to Custom Collection

There is a situation where you want to have your json into a user defined collection. In this article we will see simple code which will convert json to custom collection.

Step 1: Create a class map and name it as “EmployeeDetails.css” as shown below:

public class EmployeeDetails
{
	public string employeeName { get; set; }
}

Add following reference in your module:

using Newtonsoft.Json.Linq;

Code example

string json = @"{
'employeeDetails': [
{
  'employeeName': 'VBAOVERALL'
},
{
  'employeeName': 'INFOEXTRACT'
}
]
}";

//Parse json
JObject objEmp = JObject.Parse(json);

//Deserialize json into jArray
JArray objEmpList = (JArray)objEmp["employeeDetails"];

//Populate JArray into collection
IList<EmployeeDetails> employee = objEmpList.ToObject<IList<EmployeeDetails>>();

Console.WriteLine(employee[0].employeeName);
// VBAOVERALL

Console.WriteLine(employee[1].employeeName);
// INFOEXTRACT

Please leave your comments or queries under comment section and subscribe if you like. Next>> Json to Value Conversion C# example

Leave a Reply

Your email address will not be published. Required fields are marked *