I have the following Json response:
{
"Customers": [
{
"Customer": {
"Address": {
"City": "Stockholm",
"PostalCode": "10123"
},
"Classifications": [
"LoyaltyProgram",
"Returning",
"VeryImportant"
],
"FirstName": "Peter",
"LastName": "Centers",
"Passport": {
"Expiration": "2019-01-14",
"Number": "1564931321655"
},
},
"FirstName": "Peter",
"LastName": "Centers",
"Reservation": {
"AdultCount": 2,
"AssignedSpaceId": "03f59360-8644-4e29-927a-ad85a6514466",
},
"RoomNumber": "302"
},
]
}
I have the following classes for every Customer:
public class CustomerDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<string> Classifications { get; set; }
public PassportDto Passport { get; set; }
public AddressDto Address { get; set; }
}
public class AddressDto
{
public string City { get; set; }
public string PostalCode { get; set; }
}
public class PassportDto
{
public string Expiration { get; set; }
public string Number { get; set; }
}
from this I use Json.Net and the following code from one of my methods (extract) where customers below is the response:
var jsonCustomers = JObject.Parse(customers)["Customers"].Children().ToList();
IList<CustomerDto> customerList = new List<CustomerDto>();
foreach (var item in jsonCustomers) {
customerList.Add(item.ToObject<CustomerDto>());
}
All the values in CustomerDto are filled except for Address and Passport, that are null, and I can't figure out why.