-2

I need to create a json inside another json, I'm creating the json like this:

Turnover turnover = new Turnover();
                    turnover.DealerUserName = username;
                    turnover.CardNumber = cardnumber;
                    turnover.InvoiceNumber = invoicenumber;
                    turnover.Amount = total;
                    turnover.Currency = currency;
                    turnover.InvoiceDate = tempo;
                    turnover.SegmentNumber = segment;
                    string json = JsonConvert.SerializeObject(turnover);

This is how I create the Turnover class:

public class Turnover
    {
        public string DealerUserName { get; set; }
        public long CardNumber { get; set; }
        public string InvoiceNumber { get; set; }
        public decimal Amount { get; set; }
        public string Currency { get; set; }
        public string InvoiceDate { get; set; }
        public short SegmentNumber { get; set; }
    }

But I need to create a variable in Turnover with the features that are in the following class:

  public class product
    {
        public string ProductName { get; set; }
        public string ProductGroup { get; set; }
        public long Code { get; set; }
        public decimal Amout { get; set; }
        public int Quantity { get; set; }
        public string Referece { get; set; }
    }

How can I do that ? I was trying to do it with a list like this :

public class Turnover
    {
public List<product> TurnoverDetails { get; set; } 
    }
Turnover turnover = new Turnover();
turnover.TurnoverDetails.Add(new product() { ProductName = "", ProductGroup = "", Amout = 0, Code = 0, Quantity = 0, Referece = "", });
string json = JsonConvert.SerializeObject(turnover);

I liked json to be like this:

enter image description here

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
Jose
  • 5
  • 3

2 Answers2

2

Before adding it to TurnoverDetails list, you must create an object. please find the sample code below,

public class Turnover
{
   public Turnover()
   {
       TurnoverDetails = new List<product>();
   }
   public List<product> TurnoverDetails { get; set; } 
}
Turnover turnover = new Turnover();
turnover.TurnoverDetails.Add(new product() { ProductName = "", ProductGroup 
= "", Amout = 0, Code = 0, Quantity = 0, Referece = "", });
string json = JsonConvert.SerializeObject(turnover);
Ravi Kumar G N
  • 396
  • 1
  • 3
  • 11
0

Add property of type list

public class Turnover
        {
            public string DealerUserName { get; set; }
            public long CardNumber { get; set; }
            public string InvoiceNumber { get; set; }
            public decimal Amount { get; set; }
            public string Currency { get; set; }
            public string InvoiceDate { get; set; }
            public short SegmentNumber { get; set; }
            public List<product> Products = new List<product>();
        }

and then add product in turnover object

 Turnover turnover = new Turnover();
    turnover.Products.Add(new product() { ProductName = "", ProductGroup = "", Amout = 0, Code = 0, Quantity = 0, Referece = "", });
turnover.Products.Add(new product() { ProductName = "", ProductGroup = "", Amout = 0, Code = 0, Quantity = 0, Referece = "", });
turnover.Products.Add(new product() { ProductName = "", ProductGroup = "", Amout = 0, Code = 0, Quantity = 0, Referece = "", });


string json = JsonConvert.SerializeObject(turnover);
Sunny
  • 1,504
  • 14
  • 22