将json写入.txt文件

时间:2016-02-03 02:00:35

标签: c# json list serialization json.net

我将一个字符串列表序列化为一个Json字符串然后从json字符串生成一个文件但由于某种原因我的文件没有" {}"我的json。 这是我的列表序列化:

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

foreach(item in Model){list.Add(item)}

var reqUsers = list;
var json = JsonConvert.SerializeObject(reqUsers);

System.IO.File.WriteAllText(@"\path.txt", json);

我的路径.txt显示:

["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"]

但我需要这样的输出:

[["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"]]

我的foreach循环来填充我的列表:

&#13;
&#13;
 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.ToString());
                                                        int month = item.Fecha.Month;
                                                        if (item.Fecha.Day == i) { value = item.Cantidad; list.Add(value.ToString()); }
                                                       
                                                    }
                 list.Add(total.ToString());
                 list.Add((((costo / (total + 1)) * total).ToString("C")));
                        }
&#13;
&#13;
&#13;

我希望每次我的列表完成foreach循环的serie以使[]括起来的数据 我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

I am not sure why you need your output to be like that. But if you want it that way, you can do something like this.

        List<List<List<string> > > arrayArrayList = new List<List<List<string>>>();
        List<List<string>> arrayList = new List<List<string>>();
        List<string> list = new List<string>();

        list.Add("Hello");
        list.Add("Hello1");
        list.Add("Hello2");            
        arrayList.Add(list);
        arrayArrayList.Add(arrayList);

        list = new List<string>();
        list.Add("bye");
        list.Add("bye1");
        list.Add("bye2");
        arrayList =  new List<List<string>>();
        arrayList.Add(list);
        arrayArrayList.Add(arrayList);

        var json = JsonConvert.SerializeObject(arrayArrayList);

In this case your output will be [[["Hello","Hello1","Hello2"]],[["bye","bye1","bye2"]]]

Updated the answer based on the recent update to the question.

var buffer = new StringBuilder();
buffer.Append("[");
for(int i=1;i<=10;i++)
{
   buffer.Append("[");
   foreach(var item in items)
   {       
      buffer.Append("\"\"");
      buffer.Append(item.Pro1);         
      buffer.Append("\"\"",");
       //add other props
   }
    buffer.Append("]");
}
buffer.Append("]");
File.WriteAllText(path,buffer.ToString();

答案 1 :(得分:1)

You can wrap your current object being serialized in another collection so that it will be serialized the way you want:

var json = JsonConvert.SerializeObject(new List<object>() { list });

This should put another "[" and "]" around your current serialized text.