无法将复杂的json转换为C#类对象

问题描述:

this is my json.
<pre>{"status":200,"status_messages":"Success ","SuspendDate":null,"SubTotal":100,"Freight":0.0,"Discount":0.0,"TotalAmount":100,"CustomerCode":-1,"TableName":"TableNo2","CreatedBy":"admin","TokenNumber":"Token Number 1","MySuspendDetail":[{"RowID":1,"ProductCode":1,"RetailPrice":10,"Quantity":10,"Total":100,"TaxId":1,"ProductTax":0,"ExtraSuggestion":null,"MyModifiers":[{"ProductCode":1,"ModifierID":1,"ModifierGroupID":1,"ModifierName":"Frees","Price":0,"Quantity":10},{"ProductCode":1,"ModifierID":1,"ModifierGroupID":1,"ModifierName":"Frees","Price":0,"Quantity":10}]},{"RowID":1,"ProductCode":1,"RetailPrice":10,"Quantity":10,"Total":100,"TaxId":1,"ProductTax":0,"ExtraSuggestion":null,"MyModifiers":[{"ProductCode":1,"ModifierID":1,"ModifierGroupID":1,"ModifierName":"Frees","Price":0,"Quantity":10},{"ProductCode":1,"ModifierID":1,"ModifierGroupID":1,"ModifierName":"Frees","Price":0,"Quantity":10}]}]}









,这些是c#classe s





and these are c# classes

<pre> public class MySuspend
    {
        public long SuspendID { get; set; }
        public DateTime  SuspendDate { get; set; }
        public double  Subtotal { get; set; }
        public double Freight { get; set; }
        public double  Discount { get; set; }
        public double TotalAmount { get; set; }
        public int CustomerCode { get; set; }
        public string  TableName { get; set; }
        public string  CreatedBy { get; set; }
        public string  TokenNumber { get; set; }

    }
    public class MySuspendDetail
    {
        public long SuspendID { get; set; }
        public int RowID { get; set; }
        public long  ProductCode { get; set; }
        public double  ProductPrice { get; set; }
        public int Quantity { get; set; }
        public double Total { get; set; }
        public int TaxId { get; set; }
        public double  ProductTax { get; set; }
        public string  ExtraSuggestion { get; set; }
        List<MyModifiers> MD { get; set; }
    }
    public class MyModifiers
    {
        public long  ProductCode { get; set; }
        public int ModifierID { get; set; }
        public int ModifierGroupID { get; set; }
        public string ModifierName { get; set; }
        public double  Price { get; set; }
        public double Quantity { get; set; }
        
    }
    public class PlacingOrder
    { 
       public Collection<MySuspendDetail> SuspendDetailTable { get; set; }
        //public Collection<MyModifiers> ModifierTable { get; set; }

    }
    public class OrderPlace
    {
        public long  SuspendID { get; set; }
        public int CustomerID { get; set; }
        public string TableName { get; set; }
        public string CreatedBy { get; set; }
        public string TokenNumber { get; set; }
        public List<MySuspendDetail> data { get; set; }
    }



i希望将json反序列化为这些类

i尝试使用以下代码进行反序列化


i want to deserialized json into these classes
i have tried the following code to deserialize






我的尝试:



i尝试使用以下代码反序列化json



What I have tried:

i have tried the following code to deserialize json

public bool Ordering()
       {
           try
           {
              string  st = "{'status':200,'status_messages':'Success ','SuspendDate':null,'SubTotal':100,'Freight':0.0,'Discount':0.0,'TotalAmount':100,'CustomerCode':-1,'TableName':'TableNo2','CreatedBy':'admin','TokenNumber':'Token Number 1','MySuspendDetail':[{'RowID':1,'ProductCode':1,'RetailPrice':10,'Quantity':10,'Total':100,'TaxId':1,'ProductTax':0,'ExtraSuggestion':null,'MyModifiers':[{'ProductCode':1,'ModifierID':1,'ModifierGroupID':1,'ModifierName':'Frees','Price':0,'Quantity':10},{'ProductCode':1,'ModifierID':1,'ModifierGroupID':1,'ModifierName':'Frees','Price':0,'Quantity':10}]},{'RowID':1,'ProductCode':1,'RetailPrice':10,'Quantity':10,'Total':100,'TaxId':1,'ProductTax':0,'ExtraSuggestion':null,'MyModifiers':[{'ProductCode':1,'ModifierID':1,'ModifierGroupID':1,'ModifierName':'Frees','Price':0,'Quantity':10},{'ProductCode':1,'ModifierID':1,'ModifierGroupID':1,'ModifierName':'Frees','Price':0,'Quantity':10}]}]}";
               JavaScriptSerializer js = new JavaScriptSerializer();
               OrderPlace OR = js.Deserialize<OrderPlace>(st);


               //MySuspend sp = new MySuspend ();
               //sp.SuspendID=OR.


               //
               MySuspend ms = new MySuspend();
               ms.SuspendID = GetMaxSuspendID();
               ms.SuspendDate =System.DateTime. Now;
               ms.Subtotal = 0.0;
               ms.CreatedBy = OR.CreatedBy;
               ms.CustomerCode = OR.CustomerID;
               List<MySuspendDetail> datasubitems =OR.data  ;


               return true;
           }
           catch(Exception e)
           {
               return false;
           }
       }



但在


but get null in

datasubitems 

中得到null。

您的数据不包含数据属性。

更改此项:
Your data does not contain a "data" property.
Change this:
public List<MySuspendDetail> data { get; set; }

对此:

public List<MySuspendDetail> MySuspendDetail { get; set; }



这个:


And this:

List<MySuspendDetail> datasubitems =OR.data

对此:

List<MySuspendDetail> datasubitems = OR.MySuspendDetail

datasubitems 将不再是 null