I would like to know if it is possible to map from a json object to a poco object.
The json object I am trying to deserialize and map:
{
"customers": [{
"customerid": "",
"firstname": "",
"lastname": "",
"companyname": "",
"email": "",
"language": "",
"culture": "",
"addressline1": "",
"addressline2": "",
"city": "",
"country": "",
"phonenumber": "",
"postalcode": "",
"region": "",
"state": "",
"domain": "",
"partnerid": "",
"subscriptions": [{
"id": "",
"offerid": "",
"offername": "",
"friendlyname": "",
"quantity": "",
"parentsubscriptionid": "",
"creationdate": "",
"effectivestartdate": "",
"commitmentenddate": "",
"status": "",
"autorenewenabled": "",
"billingtype": "",
"partnerbillingcycle": "",
"partnerid": "",
"orderid": "",
"offerlink": "",
"parentsubscriptionlink": ""
}]
},
{
"customerid": "",
"firstname": "",
"lastname": "",
"companyname": "",
"email": "",
"language": "",
"culture": "",
"addressline1": "",
"addressline2": "",
"city": "",
"country": "",
"phonenumber": "",
"postalcode": "",
"region": "",
"state": "",
"domain": "",
"partnerid": "",
"subscriptions": [{
"id": "",
"offerid": "",
"offername": "",
"friendlyname": "",
"quantity": "",
"parentsubscriptionid": "",
"creationdate": "",
"effectivestartdate": "",
"commitmentenddate": "",
"status": "",
"autorenewenabled": "",
"billingtype": "",
"partnerbillingcycle": "",
"partnerid": "",
"orderid": "",
"offerlink": "",
"parentsubscriptionlink": ""
}]
},
{
"customerid": "",
"firstname": "",
"lastname": "",
"companyname": "",
"email": "",
"language": "",
"culture": "",
"addressline1": "",
"addressline2": "",
"city": "",
"country": "",
"phonenumber": "",
"postalcode": "",
"region": "",
"state": "",
"domain": "",
"partnerid": "",
"subscriptions": [{
"id": "",
"offerid": "",
"offername": "",
"friendlyname": "",
"quantity": "",
"parentsubscriptionid": "",
"creationdate": "",
"effectivestartdate": "",
"commitmentenddate": "",
"status": "",
"autorenewenabled": "",
"billingtype": "",
"partnerbillingcycle": "",
"partnerid": "",
"orderid": "",
"offerlink": "",
"parentsubscriptionlink": ""
}]
}
]
}
the DTO that I am trying to map to
public class CustomersDTO
{
public List<CustomerDTO> Customers { get; set; }
}
public class CustomerDTO
{
public BE.Customer Customer { get; set; }
public List<BE.Subscription> Subscriptions { get; set; }
}
and the mapping I am using
CreateMap<JObject, CustomersDTO>()
.ForMember("Customers", cfg => { cfg.MapFrom(jo => jo["customers"]); })
;
CreateMap<JObject, BE.Customer>()
.ForMember("CustomerGUID", cfg => { cfg.MapFrom(jo => jo["customerid"]); })
.ForMember("FirstName", cfg => { cfg.MapFrom(jo => jo["firstname"]); })
.ForMember("Surname", cfg => { cfg.MapFrom(jo => jo["lastname"]); })
.ForMember("CompanyName", cfg => { cfg.MapFrom(jo => jo["companyname"]); })
.ForMember("Email", cfg => { cfg.MapFrom(jo => jo["email"]); })
.ForMember("PreferredLanguage", cfg => { cfg.MapFrom(jo => jo["language"]); })
.ForMember("PreferredCurrency", cfg => { cfg.MapFrom(jo => jo["culture"]); })
.ForMember("Address1", cfg => { cfg.MapFrom(jo => jo["addressline1"]); })
.ForMember("Address2", cfg => { cfg.MapFrom(jo => jo["addressline2"]); })
.ForMember("Address3", cfg => { cfg.MapFrom(jo => jo["city"]); })
.ForMember("Address4", cfg => { cfg.MapFrom(jo => jo["state"]); })
.ForMember("MobileNumber", cfg => { cfg.MapFrom(jo => jo["phonenumber"]); })
.ForMember("PostalCode", cfg => { cfg.MapFrom(jo => jo["postalcode"]); })
.ForMember("Region", cfg => { cfg.MapFrom(jo => jo["region"]); })
.ForMember("CSPDomain", cfg => { cfg.MapFrom(jo => jo["domain"]); })
;
CreateMap<JObject, BE.Subscription>()
.ForMember("OfferId", cfg => { cfg.MapFrom(jo => jo["offerid"]); })
.ForMember("OfferId", cfg => { cfg.MapFrom(jo => jo["offerid"]); })
.ForMember("Quantity", cfg => { cfg.MapFrom(jo => jo["quantity"]); })
.ForMember("FriendlyName", cfg => { cfg.MapFrom(jo => jo["friendlyname"]); })
.ForMember("AssignedDate", cfg => { cfg.MapFrom(jo => jo["creationdate"]); })
.ForMember("Status", cfg => { cfg.MapFrom(jo => jo["status"]); })
.ForMember("OfferURI", cfg => { cfg.MapFrom(jo => jo["offerlink"]); })
;
public class AutoMapperConfiguration
{
public MapperConfiguration Configure()
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile<CustomerProfile>();
});
return config;
}
}
the code I am trying to execute
var customersJsonObj = JObject.Parse(jsonText);
var customers = Mapper.Map<CustomersDTO>(customersJsonObj);
When I execute the line above the CustomersDTO.Customers property has the correct number of customer objects from the json array but the nested CustomerDTO.Customer and CustomerDTO.Subscriptions properties are null.
I am not sure if I am doing this correctly I need these properties populated with the correct values from the json object.