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<clsEmployee> EmployeeList { 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
}
|
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; }
}
|