2017-07-31 206 views
3

我想知道是否可以将json对象映射到poco对象。将JSON对象映射到POCO

JSON对象我试图反序列化与地图:

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

,我试图映射到

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; }  
} 

和DTO映射我使用

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; 
    } 
} 

我试图执行的代码

var customersJsonObj = JObject.Parse(jsonText); 
var customers = Mapper.Map<CustomersDTO>(customersJsonObj); 

当我执行CustomersDTO.Customers属性上面的行时,json数组中有正确数量的客户对象,但嵌套的CustomerDTO.Customer和CustomerDTO.Subscriptions属性为null。

我不知道如果我正确地做到这一点,我需要这些属性填充从json对象正确的值。

+0

https://stackoverflow.com/questions/21611674/how-to-auto-generate-ac-sharp-class -json-object-string 我建议安装WebEssential扩展,然后你可以使用Newtonsoft JSON或任何其他库来将json字符串转换为poco对象。 –

+0

您可以使用Newtownsoft或其他框架将您的JSON反序列化为C#类。这与AutoMapper无关。当你把你的模型转换成你的数据库对象时,你会使用它。 – krillgar

+1

您的JSON首先无效,请首先验证并更正JSON。 – Aby

回答

1

这是从您的JSON创建的C#类。用这种方法,尝试映射 - (你可以用http://json2csharp.com你的JSON转换成C#代码)

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; } 
} 
+0

@Ahbay Dixit如果我已经有了我想要映射的POCO对象而不是创建新的类,我该怎么做? –

+0

你可以使用JSON.net - var obj = JsonConvert.DeserializeObject (jsonstring); – Aby

+0

如果来自源json对象的属性名称与目标poco对象不同,那么反序列化如何知道要在源和目标之间映射哪些属性? –