2011-07-22 81 views
0

我正在使用ASP.Net MVC和JSON.Net API。 我想反序列化兩個對象,比如說:CustomerOrderHeader和一個CustomerOrderLines集合。 我有JSON在jsonString變量:JSON使用Json.NET C使用一個JSON消息反序列化兩個對象#

{ 
    "customerOrderHeader": { 
     "OrderNumber": "", 
     "CustomerName": "", 
     "Reference": "", 
     "ShippingDate": "", 
     "CurrencyId": "0", 
     "LanguageId": "0", 
     "DaLine1": "", 
     "DaLine2": "", 
     "DaCity": "", 
     "DaPostalCode": "", 
     "DaState": "", 
     "DaCountry": "", 
     "BaLine1": "", 
     "BaLine2": "", 
     "BaCity": "", 
     "BaPostalCode": "", 
     "BaState": "", 
     "BaCountry": "", 
     "Notes": "" 
    }, 
    "customerOderLinesModel": [ 
     { 
      "Num": "10", 
      "PartNumber": "Inventory1", 
      "Description": "aasss", 
      "OrderedQuantity": "0", 
      "UoM": "cm", 
      "SalesPrice": "123.0000", 
      "Amount": "0.0000", 
      "LineStatus": "Draft", 
      "ConfirmedShippingDate": "", 
      "ReservedQuantity": "0", 
      "DeliveredQuantity": "0", 
      "RemainingQuantity": "0" 
     } 
    ] 
} 

和我有類:

CustomerOrderModel { 

     public string BillingAddress 
     { get; set; } 

     public string ShippingAddress 
     { get; set; } 

     [DisplayName("Customer name*:")] 
     [Required] 
     public string CustomerName 
     { get; set; } 

     [DisplayName("Order Number :")] 
     public long? OrderNumber 
     { get; set; } 

     [DisplayName("Order Status :")] 
     public int OrderStatus 
     { get; set; } 

     public string OrderStatusName 
     { get; set; } 

     [DisplayName("Shipping Date :")] 
     public DateTime? ShippingDate 
     { get; set; } 

     public bool IsDefaultda 
     { get; set; } 

     [DisplayName("Total Amount :")] 
     public decimal TotalAmount 
     { get; set; } 

     [DisplayName("Address Line 1 :")] 
     public string DaLine1 
     { get; set; } 

     [DisplayName("Address Line 2 :")] 
     public string DaLine2 
     { get; set; } 

     [DisplayName("City :")] 
     public string DaCity 
     { get; set; } 

     [DisplayName("Postal Code :")] 
     public string DaPostalCode 
     { get; set; } 

     [DisplayName("State :")] 
     public string DaState 
     { get; set; } 

     [DisplayName("Country :")] 
     public string DaCountry 
     { get; set; } 

     [DisplayName("Address Line 1 :")] 
     public string BaLine1 
     { get; set; } 

     [DisplayName("Address Line 2 :")] 
     public string BaLine2 
     { get; set; } 

     [DisplayName("City :")] 
     public string BaCity 
     { get; set; } 

     [DisplayName("Postal Code :")] 
     public string BaPostalCode 
     { get; set; } 

     ... 
} 

public class CustomerOrderLineModel 
    { 
     public CustomerOrderLineModel() 
     { 
     } 

     public String OrderNumber { get; set; } 
     public String Num { get; set; } 

     public String PartNumber { get; set; } 
     public String Description { get; set; } 
     public String OrderedQuantity { get; set; } 
     public String UoM { get; set; } 
     public String SalesPrice { get; set; } 
     public String Amount { get; set; } 
     public String LineStatus { get; set; } 
     public DateTime? ConfirmedShippingDate { get; set; } 
     public String ReservedQuantity { get; set; } 
     public String DeliveredQuantity { get; set; } 
     public String RemainingQuantity { get; set; } 


    } 

我只是想要做的就是讓JSON和通到這個類別:

JObject jObject = JObject.Parse(jsonString); 
    JToken jToken = jObject.SelectToken("customerOrderHeader"); 

現在我想使這個jToken PassTo CustomerOrderModel類, 喜歡的東西:

CustomerOrderModel customerOrderModel = (CustomerOrderModel) jObject.SelectToken("customerOrderHeader"); 

同樣以CustomerOrderLineModels的集合:

List<CustomerOrderLineModel> customerOrderLines = (List<CustomerOrderLineModel>) jObject.SelectToken("customerOderLinesModel"); 

但我不能這樣做,因爲它返回串。

感謝,

鐵托

回答

0

你需要反序列化。 http://json.codeplex.com/是我一直使用的序列化程序。它非常靈活和快速。 你可以這樣稱呼它:

CustomerOrderModel customerOrderModel = 
    json.Deserialize<CustomerOrderModel>(jObject.SelectToken("customerOrderHeader")); 
相關問題