0

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);
6
  • What is your class EInvoiceModel Commented Feb 8, 2022 at 11:11
  • invoicelined= data[i].invoiceLines; perhaps? is a blind guess, as @PeterSmith said it depends on your class and data structure Commented Feb 8, 2022 at 11:13
  • In your loop you are overwriting that invoicelined.parmItemId. You may want to add it to a list. Also, are you sure you need the same index for data and invoiceLines in data[i].invoiceLines[i]? Commented Feb 8, 2022 at 11:16
  • Thank you for your responses. Please find my EInvoiceModel contains below structure, Which i designed as per my json response. 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; } } Commented Feb 8, 2022 at 11:30
  • 1
    @Faqruddin please update your question instead of adding it as comment Commented Feb 8, 2022 at 11:31

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

12 Comments

Still its returning one value only. Its not returning all parmItemId inside that list.
Could you please share JSON file structure?
Please check I updated.
Could you please check the updated answer?
No records returned :)
|
0

You now have this:

EInvoiceModel.Class1 ds = new EInvoiceModel.Class1();

for (int i = 0; i < data.Count; i++)
{
    ds.invoiceLines = new List<EInvoiceModel.Invoiceline>
       {
           new EInvoiceModel.Invoiceline
           {
              parmItemId = data[i].invoiceLines[i].parmItemId
           }
        };
}

This means that on every iteration you overwrite that ds.invoiceLines with a new list, containing a single item. So you end up with only the last one.

A possibly better way would be this:

EInvoiceModel.Class1 ds = new EInvoiceModel.Class1();
ds.invoiceLines = new List<EInvoiceModel.Invoiceline>(); // create a list once

for (int i = 0; i < data.Count; i++)
{
    // add one item to that list
    ds.invoiceLines.Add(new EInvoiceModel.Invoiceline
           {
              parmItemId = data[i].invoiceLines[i].parmItemId
           }
        );
}

This way you assign a new (empty) list once (you can skip this when invoiceLines already contains an empty list, after you created a new Class1).

And then, in the loop, you don't assign a new list, but just add a new item to that existing list. At the end, you get a list with as many items as there are in your input data.


By the way, this assumes that you really need to make a list of data[i].invoiceLines[i].parmItemId. I find it curious that for data[3] you need an invoiceLines[3] and for data[7] an invoiceLines[7]. I would expect one loop through all data and then an inner loop over all its invoiceLines. But you know your data best.


Apparently, you do need to loop over the inner objects. So modify that code like this:

EInvoiceModel.Class1 ds = new EInvoiceModel.Class1();
ds.invoiceLines = new List<EInvoiceModel.Invoiceline>(); // create a list once

// loop over all "data" items
for (int i = 0; i < data.Count; i++)
{
    // then loop over the invoiceLines inside the current data item
    for (int j = 0; j < data[i].invoiceLines.Count(); j++)
    {
        // add one item to that list
        ds.invoiceLines.Add(new EInvoiceModel.Invoiceline
           {
              parmItemId = data[i].invoiceLines[j].parmItemId
           }
        );
    }
}

Note that I still use index i in data[i] from the outer loop, but now use index j in invoiceLines[j] from the inner loop.

1 Comment

Great Sir. I did the same. Your absolutely right. we loop data[i] but but still we need inner-loop over all its invoiceLines. Could you please add to above code inner loop over all its invoice lines. So I can repeat all lines. This will save time. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.