Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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}]}]}




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 want to deserialized json into these classes
i have tried the following code to deserialize



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 
.
Posted
Updated 13-Oct-18 0:19am
v4
Comments
OriginalGriff 13-Oct-18 5:54am    
Where is the OrderPlace class? You don't show it's definition, and since that is what you are deserializing the JSON into so it's probably very relevant.

Use the "Improve question" widget to edit your question and provide better information.
Member 13252808 13-Oct-18 6:05am    
please check now

1 solution

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

And this:
List<MySuspendDetail> datasubitems =OR.data
To this:
List<MySuspendDetail> datasubitems = OR.MySuspendDetail
And datasubitems will no longer be null
 
Share this answer
 
Comments
Member 13252808 13-Oct-18 6:27am    
i am confuse can you please crelify that where i place public List<mysuspenddetail> data { get; set; }
and where public List<mysuspenddetail> MySuspendDetail { get; set; }
Member 13252808 13-Oct-18 6:35am    
done. thank you so much dear,
OriginalGriff 13-Oct-18 6:41am    
You're welcome!

BTW: Don't use "dear" on the internet. I know it's common in India, but in parts of the western world it's seen as "patronising sexism" when used to females. Since you have no idea who (or what gender) you are talking to, it's best to leave it out altogether on the internet. Not a problem with me, but it's best to avoid accidental offence.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900