I am using restsharp and a newtonsoft.json in order to communicate with my Python API.
Communications works fine. Basic queries, which return simple strings are processed, so that's not a problem.
I have problem with deserialization of more complex results and i am doing this for the first time.
So, from web service point of view i am returning following json structure:
{
"Employee": [
{
"Department.DepartmentName": "IT",
"Employee.EmployeeArchived": 0,
"Employee.EmployeeDepartmentId": 13,
"Employee.EmployeeFired": 0,
"Employee.EmployeeId": 1,
"Employee.EmployeeName": "Name1",
"Employee.EmployeePID": 292,
"Employee.EmployeeSurname": "Surname1"
},
{
"Department.DepartmentName": "IT",
"Employee.EmployeeArchived": 0,
"Employee.EmployeeDepartmentId": 4,
"Employee.EmployeeFired": 0,
"Employee.EmployeeId": 2,
"Employee.EmployeeName": " Name2",
"Employee.EmployeePID": 50,
"Employee.EmployeeSurname": " Surname2"
}
]
}
in response.contents (result of RestClient.Execute) in my C# programm i am getting something like this (i truncated this to just one data row):
{\n \"Employee\": [\n {\n \"Department.DepartmentName\": \"EDV\", \n \"Employee.EmployeeArchived\": 0, \n \"Employee.EmployeeDepartmentId\": 13, \n \"Employee.EmployeeFired\": 0, \n \"Employee.EmployeeId\": 1, \n \"Employee.EmployeeName\": \"Name1\", \n \"Employee.EmployeePID\": 292, \n \"Employee.EmployeeSurname\": \"Surname1\"\n }
So far it looks good.
Now. I tried to define my data class to be able to deserialize it as follows:
public class Employee
{
[JsonProperty("Department.DepartmentName")]
public string Department { get; set; }
[JsonProperty("Employee.EmployeeName")]
public string Name {get; set;}
[JsonProperty("Employee.EmployeeSurname")]
public string Surname { get; set; }
[JsonProperty("Employee.EmployeeArchived")]
public int Archived { get; set; }
[JsonProperty("Employee.EmployeeFired")]
public int Fired { get; set; }
[JsonProperty("Employee.EmployeeId")]
public int Id { get; set; }
[JsonProperty("Employee.EmployeeDepartmentId")]
public int DepId { get; set; }
[JsonProperty("Employee.EmployeePID")]
public int PID { get; set; }
In addition class contains a constructor, but i assume that's not the problem.
Now, simply calling:
var Data = JsonConvert.DeserializeObject<Employee>(response.Content);
... returns a null. I suppose the problem is i return a dictionary "Employee", not just the single data row. How i would need to design it to deserialize this correctly?
Looks like it was simpler than i thought. I renamed "Employee" class to "EmployeeData" (just for sake of elegancy & compatibility with json parameter names).
then i defined second class:
class EmployeeList
{
[JsonProperty("Employee")]
public List<EmployeeData> Employee { get; set; }
}
And then
JsonConvert.DeserializeObject<EmployeeList>(response.Content);
returns correctly filled class with list of employees. Trivial. Json.net is really powerfull.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.