2015-12-15 108 views
0

由於與Newtonsoft相比,我使用ServiceStack nuget包進行JSON序列化/反序列化。我有一個數據結構,其中包含了一些特性,這是自定義對象的列表,這裏有我的課ServiceSatck JSON國際化

public class MainBO 
{ 
    public IList<BasketItem> Items{get;set;} 
} 

public class BasketItem 
{ 
    public int BasketItemId { get; set; } 

    public string ItemId { get; set; } 

    public string Upc { get; set; } 

    public string Description { get; set; } 

    public string Name { get; set; } 

    public Decimal OrginalPrice { get; set; } 

    public Decimal CurrentPrice { get; set; } 
    public IList<Decimal> OfferPrice { get; set; } 
    public Decimal ShippingCost { get; set; } 

    public Decimal DeliveryCharge { get; set; } 
    public string ReceiptMessage { get; set; } 
    public int Quantity { get; set; } 

    public bool? Discountable { get; set; } 

    public bool? Taxable { get; set; } 

    public bool IsPriceOveriddedItem { get; set; } 

    public string Taxcode { get; set; } 

    public string PriceOverrideReason { get; set; } 

    public string TaxOverrideReason { get; set; } 

    public int OriginalQuantity { get; set; } 

    public int RemainingQuantity { get; set; } 
    public IList<string> Hierarchy { get; set; } 

    public IList<LineDiscountBO> AppliedDiscounts { get; set; } 

    public IList<LineTaxBO> AppliedTaxes { get; set; } 

    public BasketItemStatus Status { get; set; } 

    public decimal EffectiveTaxPercent { get; set; } 

    public string ProductImage { get; set; } 

    public bool ShippingRequired { get; set; } 
    public string ReturnProductReason { get; set; } 
    public string OtherReason { get; set; } 
} 



public class LineTaxBO 
{ 
    public long TaxId { get; set; } 
    public string TaxClassId { get; set; } 
    public Decimal Amount { get; set; } 

    public Decimal Percentage { get; set; } 

    public string TaxOverrideReason { get; set; } 
} 

public class LineDiscountBO 
    { 
    public long DiscountId { get; set; } 
    public Decimal Amount { get; set; } 
} 

,我試圖序列化一個JSONObject其中包含的數據

{"Items":[{"BasketItemId":1,"ItemId":"SK1XXX78","Upc":"671873084895","Name":"HTC 620","OrginalPrice":12,"CurrentPrice":8.4,"OfferPrice":[8.4],"ShippingCost":1.1,"DeliveryCharge":0,"ReceiptMessage":"Buy 1 Get 30% 2 Get 40% 3 Get 50% Discount","Quantity":1,"Discountable":true,"Taxable":true,"IsPriceOveriddedItem":false,"Taxcode":"12","OriginalQuantity":0,"RemainingQuantity":1,"Hierarchy":["760","760-001","760-001-002","760-001-002-001","760-001-002-001-YLGSNTSH","760-001-002-001-YLGSNTSH-10526160"],"AppliedTaxes":[{"TaxId":0,"TaxClassId":"12","Amount":0.25,"Percentage":3}],"Status":"Added","EffectiveTaxPercent":3,"ProductImage":"Mobiles\\6.jpg","ShippingRequired":false},{"BasketItemId":2,"ItemId":"SKXXX08","Upc":"400000331621","Name":"Wings of fire","OrginalPrice":9,"CurrentPrice":9,"OfferPrice":[9],"ShippingCost":1.1,"DeliveryCharge":0,"Quantity":1,"Discountable":false,"Taxable":true,"IsPriceOveriddedItem":false,"Taxcode":"11","OriginalQuantity":0,"RemainingQuantity":1,"Hierarchy":["600","600-001","600-001-001","600-001-001-001","600-001-001-001-PPPSHIRT1","600-001-001-001-PPPSHIRT1-90013155"],"AppliedTaxes":[{"TaxId":0,"TaxClassId":"11","Amount":0.18,"Percentage":2}],"Status":"Added","EffectiveTaxPercent":2,"ProductImage":"Books\\1.jpg","ShippingRequired":false}],"TotalAppliedDiscount":3.6,"TotalAppliedTax":0.43,"TotalApplicableTaxes":[{"TaxClass_Id":"12","Amount":0.25},{"TaxClass_Id":"11","Amount":0.18}],"ClientID":"'523e64ea-7748-48f6-94af-5433a2909bc2'","Total":17.83,"SubTotal":17.4,"AmountPaid":17.83,"BalanceDue":0,"Tenders":[{"TenderModeId":1,"TenderMode":"Cash","TenderedAmount":17.83}],"CustomerId":0,"IsReturnTransaction":false,"IndividualQuantityDisplay":false,"HasPromotion":true,"IsMember":false,"IsAssosiate":false,"TerminalId":"100","StoreId":"1001","ShippingAndHandlingCharge":0,"NumberOfItems":2} 

這裏是我的序列化方法,我不知道如何設置的值的層次

public static BasketBO DeserializeBasket(ServiceStack.Text.JsonObject data)//JObject data) 
    { 

     var basketBo = new MainBO; 
     basketBo.Items = data.ArrayObjects("Items").ConvertAll<BasketItem>(x => new BasketItem 
     { 
      BasketItemId = Convert.ToInt32(x["BasketItemId"]), 
      CurrentPrice = Convert.ToDecimal(x["CurrentPrice"]), 
      OriginalQuantity = Convert.ToInt32(x["OriginalQuantity"]), 
      ItemId = x["ItemId"], 
      Upc = x["Upc"], 
      Quantity = Convert.ToInt32(x["Quantity"]), 
      Name = x["Name"], 
      Taxable = Convert.ToBoolean(x["Taxable"]), 
      Taxcode = x["Taxcode"], 
      TaxOverrideReason = x["TaxOverrideReason"], 
      ShippingCost = Convert.ToDecimal(x["ShippingCost"]), 
      AppliedTaxes = x.ArrayObjects("AppliedTaxes") != null ? x.ArrayObjects("AppliedTaxes").ConvertAll<LineTaxBO>(tax => new LineTaxBO 
      { 
       Amount = Convert.ToDecimal(tax["Amount"]), 
       Percentage = Convert.ToDecimal(tax["Percentage"]), 
       TaxClassId = tax["TaxClassId"], 
       TaxId = Convert.ToInt64(tax["TaxId"]), 
       TaxOverrideReason = tax["TaxOverrideReason"] 

      }) : null, 
      AppliedDiscounts = x.ArrayObjects("AppliedDiscounts") != null ? x.ArrayObjects("AppliedDiscounts").ConvertAll<LineDiscountBO>(discount => new LineDiscountBO 
      { 
       Amount = Convert.ToDecimal(discount["Amount"]), 
       DiscountId = Convert.ToInt64(discount["DiscountId"]) 

      }) : null, 
      Hierarchy = x.ArrayObjects("Hierarchy").ConvertAll<string>(str => str.ToString()), 
     }); 



     return basketBo; 
    } 

回答

0

這有幾個問題,您應該避免使用像接口哪一個是highly discouraged anti-pattern on DTO's。另一個問題是,默認情況下只ServiceStack.Text序列化器默認公共屬性所以你應該改變你的序列化模式:

public class MainBO 
{ 
    List<TaxItem> Taxes { get; set; } 
    List<string> Parentlevels { get; set; } 
} 

否則,您可以配置ServiceStack連載公共領域有:

JsConfig.IncludePublicFields = true; 
+0

我忘了提及我的所有屬性都是公共的,只有getter和setter。即我的班級是公共類美寶 { 公共稅收的IList {獲得;設置;} 公共Parentlevels的IList {獲得;設置;} ---其他一些標準的類型屬性 } –

+0

@jereeshthomas除非您提供代碼和json有錯誤,所以任何人都不可能識別問題並幫助你。也不要使用像IList 這樣的接口,因爲它幾乎總是隱藏一個具體的列表,所以它在實踐中是一個無用的界面,而且它在串行化中更加糟糕,因爲它只增加了耦合。還請提供**正在嘗試反序列化的**精確** JSON,您的問題顯示了屬性名稱和值中包含空格的馬虎JSON,我們無法判斷您的JSON是錯誤的還是粗心粘貼的。 – mythz

+0

我已經使用實際代碼編輯了我的帖子。 –