2017-07-31 208 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對象不同,那麼反序列化如何知道要在源和目標之間映射哪些屬性? –