Json.net序列化c#

Json.net序列化c#,c#,asp.net,json,C#,Asp.net,Json,我想让json像这样 { 'month': '201701', 'value': '170', 'target': '100' }, { 'month': '201702', 'value': '200', 'target': '200' }, { 'month': '201703', 'value': '210', '

我想让json像这样

 {
        'month': '201701',
        'value': '170',
        'target': '100'
    },
    {
        'month': '201702',
        'value': '200',
        'target': '200'
    },
    {
        'month': '201703',
        'value': '210',
        'target': '400'
    }
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];
            }
在牛顿的文献中是这样的

我遵循文档,但只得到

{
    'month': '201701',
    'value': '170',
    'target': '100'
}
我试着像这样插入我的循环

 {
        'month': '201701',
        'value': '170',
        'target': '100'
    },
    {
        'month': '201702',
        'value': '200',
        'target': '200'
    },
    {
        'month': '201703',
        'value': '210',
        'target': '400'
    }
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];
            }
SellTrhu产品=新的SellTrhu();
对于(int i=1;i y.Month==Month[i]),选择(x=>x.NetAmount±0).Sum();
targetAmount[i]=_context.DashboardSellThruSummary.Where(y=>y.Month==Month[i])。选择(x=>x.Ach±0.Sum();
product.month=月[i];
产品价值=金额[i];
product.target=targetAmount[i];
}

但它返回错误

我想这就是你想要的

        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);
        }
List products=newlist();
对于(int i=1;i y.Month==Month[i]),选择(x=>x.NetAmount±0).Sum();
targetAmount[i]=_context.DashboardSellThruSummary.Where(y=>y.Month==Month[i])。选择(x=>x.Ach±0.Sum();
product.month=月[i];
产品价值=金额[i];
product.target=targetAmount[i];
产品。添加(产品);
}

现在序列化产品

您是否已将其添加到列表中。它应该是一个项目集合。需要序列化的产品集合。现在您可能正在序列化单个对象。您应该将产品包含在for循环中。然后将产品添加到产品列表中,然后在文章中序列化示例输出不代表有效的JSON,因此您需要使用单个块的字符串串联手动构建它。。。C#样本非常奇怪,并没有澄清问题……你们得到了什么错误?我还注意到您没有初始化amount和targetAmount。另一个问题是,每次循环迭代都会覆盖产品变量。