Json.net序列化c#

时间:2017-10-12 04:26:37

标签: c# asp.net json

我想让json像这样

 {
        'month': '201701',
        'value': '170',
        'target': '100'
    },
    {
        'month': '201702',
        'value': '200',
        'target': '200'
    },
    {
        'month': '201703',
        'value': '210',
        'target': '400'
    }
在newtons文档中的

就像这个https://www.newtonsoft.com/json/help/html/SerializeObject.htm

我遵循文档但只获得

{
    'month': '201701',
    'value': '170',
    'target': '100'
}

我尝试像这样插入我的循环

SellTrhu product = new SellTrhu();
            for (int i = 1; i <= 8; i++)
            {
                double[] month = new double[8];
                month[i] = 201700 + i;
                amount[i] = _context.VGetSellThruSales.Where(y => y.Month == month[i]).Select(x => x.NetAmount ?? 0).Sum();
                targetAmount[i] = _context.DashboardSellThruSummary.Where(y => y.Month == month[i]).Select(x => x.Ach ?? 0).Sum();

                product.month = month[i];
                product.value = amount[i];
                product.target = targetAmount[i];
            }

但它返回错误

1 个答案:

答案 0 :(得分:1)

我认为这就是你想要的。

        List<SellTrhu> products = new List<SellTrhu>();
        for (int i = 1; i <= 8; i++)
        {
            SellTrhu product = new SellTrhu();
            double[] month = new double[8];
            month[i] = 201700 + i;
            amount[i] = _context.VGetSellThruSales.Where(y => y.Month == month[i]).Select(x => x.NetAmount ?? 0).Sum();
            targetAmount[i] = _context.DashboardSellThruSummary.Where(y => y.Month == month[i]).Select(x => x.Ach ?? 0).Sum();

            product.month = month[i];
            product.value = amount[i];
            product.target = targetAmount[i];
            products.Add(product);
        }

现在序列化产品