将ListObjects写入特定格式文件

时间:2016-02-02 07:44:22

标签: c# json list file listobject

我想序列化List并以特定方式将其写入文件,但我现在无法这样做,我的文件就是这样:

  ["ENS FRUTAS",
"REST",
"CENAS",
"$26.50","0","1","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0",
"1",
"$26.50"]

但我需要它像这样

[
  {
    "Descripcion": "ENS FRUTAS","Pdv": "REST","Rid": "CENAS","PV": "$26.50","1": "0",
    "2": "1","3": "0","4": "0","5": "1","6": "0","7": "0","8": "1","9": "0","10": "0",
    "11": "1","12": "0","13": "0","14": "1","15": "0","16": "0","17": "1","18": "0",
    "19": "0","20": "1","21": "0","22": "0","23": "1","24": "0","25": "0","26": "1",
    "27": "0","28": "0","29": "0","30": "1","31": "0","Total": "1","Venta": "$26.50",

  }]

如何让我的文件像第二种格式? 这是我的序列化行动:

 List<object> list = new List<object>();

                foreach (var item in db.Pos)
                   {
                        var total = 0;
                        decimal costo = 0;

                       for (int i = 1; i <= 31; i++)
                            {
                            var value = 0;
                             if (item.Fecha.Day == i) { value = item.Cantidad; costo = costo + item.Total; }
                                                        total += value;
                             }
                                                       list.Add(item.Descripcion);
                                                       list.Add(item.Pdv);
                                                       list.Add(item.Rid);
                                                       list.Add(((costo / (total + 1)).ToString("C")));
                                                        for (int i = 1; i <= 31; i++)
                                                        {
                                                            var value = 0;
                                                            list.Add(value);
                                                            int month = item.Fecha.Month;
                                                            if (item.Fecha.Day == i) { value = item.Cantidad; list.Add(value); }

                                                        }
                     list.Add(total);
                     list.Add((((costo / (total + 1)) * total).ToString("C")));
                            }
 var javaScriptSerializer = new
            System.Web.Script.Serialization.JavaScriptSerializer();
            string jsonString = javaScriptSerializer.Serialize(list);
            System.IO.File.WriteAllText(@"C:\path.txt", jsonString);

0 个答案:

没有答案