JSON Object Map for POCO

I would like to know if the json object can be mapped to the poco object.

The json object I'm trying to deserialize and display:

    {
    "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": ""
            }]
        }
    ]
}

      

DTO 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 display 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 a row over the CustomersDTO.Customers property, it has the correct number of customer objects from the json array, but the CustomerDTO.Customer and CustomerDTO.Subscriptions nested properties are null.

I'm not sure if I am doing this correctly. I need these properties filled with the correct values ​​from a json object.

+3


source to share


1 answer


Here is a C # class generated from your JSON. Using this try matching - (you can use http://json2csharp.com to convert your JSON to C # code)



public class Subscription
{
    public string id { get; set; }
    public string offerid { get; set; }
    public string offername { get; set; }
    public string friendlyname { get; set; }
    public string quantity { get; set; }
    public string parentsubscriptionid { get; set; }
    public string creationdate { get; set; }
    public string effectivestartdate { get; set; }
    public string commitmentenddate { get; set; }
    public string status { get; set; }
    public string autorenewenabled { get; set; }
    public string billingtype { get; set; }
    public string partnerbillingcycle { get; set; }
    public string partnerid { get; set; }
    public string orderid { get; set; }
    public string offerlink { get; set; }
    public string parentsubscriptionlink { get; set; }
}

public class Customer
{
    public string customerid { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string companyname { get; set; }
    public string email { get; set; }
    public string language { get; set; }
    public string culture { get; set; }
    public string addressline1 { get; set; }
    public string addressline2 { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string phonenumber { get; set; }
    public string postalcode { get; set; }
    public string region { get; set; }
    public string state { get; set; }
    public string domain { get; set; }
    public string partnerid { get; set; }
    public List<Subscription> subscriptions { get; set; }
}

public class RootObject
{
    public List<Customer> customers { get; set; }
}

      

+1


source







All Articles