How to get or extract the data from different json format in c#, webapi

                        JSON
 C# Code
 Model Class
Sample 1:
{
   "EmployeeList": [
      {
         "EmpNumber": "10029597-3",
         "JobCode": "03363-001",
         "Location": "Triad North"
      },
      {
         "EmpNumber": "10029600-1",
         "JobCode": "03363-001",
         "Location": "Triad North"
      },
      {
         "EmpNumber": "10029626-2",
         "JobCode": "03363-001",
         "Location": "Triad North"
      },
      {
         "EmpNumber": "10029629-1",
         "JobCode": "03363-001",
         "Location": "Triad North"
      }
   ]
}
Type 1:

 public void JsonChecker(clsEmployeeList json)
        {
            //Do Something          
        }

public class clsEmployee
{
        public string EmpNumber{ get; set; }
        public string JobCode { get; set; }
        public string Location{ get; set; }
}

public class clsEmployeeList
{
        public List<clsEmployeeEmployeeList { get; set; }
}

Sample 2:
[
   {
      "EmpNumber": "10029597-3",
      "JobCode": "03363-001",
      "Location": "Triad North"
   },
   {
      "EmpNumber": "10029600-1",
      "JobCode": "03363-001",
      "Location": "Triad North"
   },
   {
      "EmpNumber": "10029626-2",
      "JobCode": "03363-001",
      "Location": "Triad North"
   },
   {
      "EmpNumber": "10029629-1",
      "JobCode": "03363-001",
      "Location": "Triad North"
    }
]
Type 1:
 public void JsonChecker(clsEmployee[] json)
        {
            //Do Something 
        }

Type 2:
 public void JsonChecker(List<clsEmployee>  json)
        {
           //Do Something   
        }


public class clsEmployee
{
        public string EmpNumber{ get; set; }
        public string JobCode { get; set; }
        public string Location{ get; set; }
}
Sample 3:
{
   "EmpNumber": "10029597-3",
   "JobCode": "03363-001",
   "Location": "Triad North"
}
Type 1:
public void JsonChecker(clsEmployee json)
        {
           //Do Something    
        }
public class clsEmployee
{
        public string EmpNumber{ get; set; }
        public string JobCode { get; set; }
        public string Location{ get; set; }
}
Sample 4:
{
   "Addresses": [
      "test1",
      "test2",
      "test3",
      "test4"
   ]
}
Type 1:
public void JsonChecker(clsEmployeeList json)
        {
           //Do Something    
        }


 public class clsEmployeeList
 {
        public List <stringAddresses { get; set; }
 }
Sample 5 :
["test1","test2","test3","test4"]
Type 1:
public void JsonChecker(string[] json)
        {
           //Do Something    
        }

Type 2:
public void JsonChecker(List<string> json)
        {
           //Do Something    
        }


Sample 6:
{
   "Name": "Abhinay Kumar",
   "Email": "Test@mail.com",
   "Addresses": [
      "test1",
      "test2",
      "test3",
      "test4"
   ],
   "pfa": [
      {
         "EmpNumber": "10029597-3",
         "JobCode": "03363-001",
         "Location": "Triad North"
      },
      {
         "EmpNumber": "10029600-1",
         "JobCode": "03363-001",
         "Location": "Triad North"
      },
      {
         "EmpNumber": "10029626-2",
         "JobCode": "03363-001",
         "Location": "Triad North"
      },
      {
         "EmpNumber": "10029629-1",
         "JobCode": "03363-001",
         "Location": "Triad North"
      }
   ]
}
Type 1:
public void JsonChecker(clsEmployeeList json)
        {
           //Do Something    
        }
public class clsEmployeeList
{
        public string Name { get; set; }
        public string Email { get; set; }

        public List<string> Addresses { get; set; }
        public List<clsEmployee> pfa { get; set; }
}

public class clsEmployee
 {
        public string EmpNumber{ get; set; }
        public string JobCode { get; set; }
        public string Location{ get; set; }
 }

Popular posts from this blog

Top MNC Interview Questions- Full Stack Developer

Interview Questions-2

How to get the user details, user current level, next level, Rank in sql server