This is a tough one: Basically I want to map a complex JSON object with a collection consisting of different but similar complex types into a C# model. This is a default JSON my ASP.NET Web API will receive:
{
"Id": "000111222333",
"Move": {
"Address2": {
"City": "CityName",
"Street": "StreetName",
"PostalCode": "4444",
"HouseNumber": "4",
"Country": "NLD",
"IsInhabited": "true"
},
"Contracts": {
"ElectricityContract": {
"ContractId": "000000031",
"ContractNumber": "00000011",
"EANCode": "53123123123123",
"StartDate": "2000-01-20",
"EndDate": "2017-06-06",
"IsBuildingComplex": "false",
"ForceSingleTariff": "false"
},
"CableContract": {
"ContractId": "456454546353",
"ContractNumber": "12312312313",
"StartDate": "2000-01-20",
"EndDate": "2017-01-23"
}
}
}
}
My problem here is that I can't seem to receive the different kinds of Contracts as a single collection.
I've tried to map the "Contracts" sequence to an ArrayList but its always 'null'. I've tried to map it to ICollection/IEnumerable/List< IContract> where all contracts are a type of IContract. But this doesn't work as well.
I've started to manually map from the JSON string, but I hope there's a better way to do this? All help appreciated.