我需要在另一个json中创建一个json,我正在创建这样的json:
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);
这就是我创建Turnover
类的方法:
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; }
}
但我需要在Turnover中使用以下类中的功能创建变量:
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; }
}
我该怎么做?我试图用list
这样做:
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);
我喜欢json是这样的:
答案 0 :(得分:2)
在将其添加到TurnoverDetails列表之前,您必须创建一个对象。请找到下面的示例代码,
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);
答案 1 :(得分:0)
添加列表类型
的属性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>();
}
然后在营业额对象中添加产品
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);