Home > Software design >  How to loop list class
How to loop list class

Time:02-08

I have below class. And data is serialized using JsonConvert.DeserializeObject. The data is returning from list. But I want to loop using for or for each. Please advice.

Thank you.

    var result = client.getInvocieLine
    (call Context);

   string strResultJson = JsonConvert.SerializeObject(result);

   System.IO.File.WriteAllText(@"D:\jsondata\invoicedata.json", strResultJson);

   string fileName = @"D:\jsondata\invoicedata.json";
   string jsonText = System.IO.File.ReadAllText(fileName)
  List<EInvoiceModel.Class1> data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EInvoiceModel.Class1>>(jsonText);

            EInvoiceModel.Invoiceline invoicelined = new EInvoiceModel.Invoiceline();

            for(int i=0; i < data.Count; i  )
            {
                invoicelined.parmItemId = data[i].invoiceLines[i].parmItemId; //Its bring only one record. I need all record from data. Please advice.
            }
            return Ok(invoicelined);
    

Please find my EInvoiceModel contains below structure, Which i designed as per my json response. yes I need same index for data and invoiceLines in data[i].invoiceLines[i]. How can I get this to return all values. Index currently pointing only one record and return one record. If I change position its return another record. But I need all please.

public class Class1
{
    public Invoiceline[] invoiceLines { get; set; }
    public string parmCustName { get; set; }
    public DateTime parmInvoiceDate { get; set; }       
}     public class Invoiceline
{           
    public string parmItemId { get; set; }
    public string parmItemNameDisplay { get; set; }
    public float parmQty { get; set; }} 

Please find my json structure. I wist to repeat all parameter-ids from json response.

{"invoiceLines":[{"parmCurrencyCode":null,"parmCustExchRate":0.0,"parmInvoiceId":null,"parmItemId":null,"parmItemNameDisplay":null,"parmQty":0.0,"parmSalesLinePercent":0.0,"parmSalesUnit":null,"parmdiscountAmount":0.0,"parmnetTotal":0.0,"parmsalesPrice":0.0,"parmsalesTotal":0.0,"parmtotalItemsLineDisc":0.0,"parmtotalTaxableFees":0.0}],"parmCustName":null,"parmInvoiceDate":"0001-01-01T00:00:00","parmInvoiceId":null,"parmSalesId":null,"parmnetAmount":0.0,"parmtotalAmount":0.0,"parmtotalAmountWithDisc":0.0,"parmtotalItemsDiscountAmount":0.0,"parmtotalSalesAmount":0.0}

Please I want return all parmItemId below way. I don't want damage my json structure.

      EInvoiceModel.Class1 ds = new EInvoiceModel.Class1();
       List<EInvoiceModel.Class1> data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EInvoiceModel.Class1>>(jsonText);
       // List<EInvoiceModel.Invoiceline> data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EInvoiceModel.Invoiceline>>(jsonText);

        for (int i = 0; i < data.Count; i  )
        {

            ds.invoiceLines = new List<EInvoiceModel.Invoiceline>
        {
             new EInvoiceModel.Invoiceline
             {
             parmItemId = data[i].invoiceLines[i].parmItemId
             }
        };
           

        }
        return Ok(ds);

CodePudding user response:

You have used to Generic list

To get all list

List<EInvoiceModel.Invoiceline> data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EInvoiceModel.Invoiceline>>(jsonText);

           List<EInvoiceModel.Invoiceline> invoicelines = new List<EInvoiceModel.Invoiceline>();

            for(int i=0; i < data.Count; i  )
            {
                invoicelines.add(new EInvoiceModel.Invoiceline () { parmItemId = data[i].parmItemId}; 
            }
            return Ok(invoicelines);

CodePudding user response:

Thank you for your responses. Please find my EInvoiceModel contains below structure, Which i designed as per my json response. yes I need same index for data and invoiceLines in data[i].invoiceLines[i]. How can I get this to return all values. Index currently pointing only one record and return one record. If I change position its return another record. But I need all please.

public class Class1
    {
        public Invoiceline[] invoiceLines { get; set; }
        public string parmCustName { get; set; }
        public DateTime parmInvoiceDate { get; set; }       
    }     public class Invoiceline
    {           
        public string parmItemId { get; set; }
        public string parmItemNameDisplay { get; set; }
        public float parmQty { get; set; }} 
     
     

CodePudding user response:

Please find my json structure. I wist to repeat all parameter-ids from json response.

{"invoiceLines":[{"parmCurrencyCode":null,"parmCustExchRate":0.0,"parmInvoiceId":null,"parmItemId":null,"parmItemNameDisplay":null,"parmQty":0.0,"parmSalesLinePercent":0.0,"parmSalesUnit":null,"parmdiscountAmount":0.0,"parmnetTotal":0.0,"parmsalesPrice":0.0,"parmsalesTotal":0.0,"parmtotalItemsLineDisc":0.0,"parmtotalTaxableFees":0.0}],"parmCustName":null,"parmInvoiceDate":"0001-01-01T00:00:00","parmInvoiceId":null,"parmSalesId":null,"parmnetAmount":0.0,"parmtotalAmount":0.0,"parmtotalAmountWithDisc":0.0,"parmtotalItemsDiscountAmount":0.0,"parmtotalSalesAmount":0.0}
  •  Tags:  
  • Related