如何在C#中添加类似于JSON格式的逗号分隔值

时间:2014-07-24 15:19:35

标签: c#

我正在尝试通过读取csv并填充数据表来为JSON兼容性创建类似于此的输出。然后我循环创建指定的格式(在下面指定)。

我正在寻找的是在开始和结束括号内添加逗号分隔值,如下面格式所示,用于checkins,teamsize和checkintimes列。但是我无法复制相同的内容。

格式:

"name":"aaa_aaaurf","region":"Financial & Risk - ALL","checkins":[[1,"0"],[2,"0"],[3,"3"],[4,"0"],[5,"0"],[6,"0"],[7,"0"],[8,"0"],[9,"0"],[10,"0"],[11,"0"],[12,"2"]],"teamsize":[[1,"2"],[2,"2"],[3,"2"],[4,"2"],[5,"0"],[6,"0"],[7,"0"],[8,"0"],[9,"0"],[10,"0"],[11,"3"],[12,"3"]],"Checkintimes":[[1,"0"],[2,"0"],[3,"0"],[4,"0"],[5,"0"],[6,"0"],[7,"0"],[8,"0"],[9,"0"],[10,"0"],[11,"0"],[12,"62"]

我实现相同的代码是:

string repository = null;
            string tempsbu;
            int index=0;
            string sbu=null;
            List<string> checkins = new List<string>();
            List<string> builds = new List<string>();
            List<string> teamsize = new List<string>();
            List<string> checkintimes = new List<string>();
            List<string> Consdata = new List<string>();

            string CSVFilePathName = @"D:\info.csv";
            string[] Lines = File.ReadAllLines(CSVFilePathName);
            string[] Fields;
            Fields = Lines[0].Split(new char[] { ',' });
            int Cols = Fields.GetLength(0);
            DataTable dt = new DataTable();
            //1st row must be column names; force lower case to ensure matching later on.
            for (int i = 0; i < Cols; i++)
                dt.Columns.Add(Fields[i].ToLower(), typeof(string));
            DataRow Row;
            for (int i = 1; i < Lines.GetLength(0); i++)
            {
                Fields = Lines[i].Split(new char[] { ',' });
                Row = dt.NewRow();
                for (int f = 0; f < Cols; f++)
                    Row[f] = Fields[f];
                dt.Rows.Add(Row);
            }


            for (int row = 0; row < dt.Rows.Count; row++)
            {
                if (row==0)
                {
                    repository = dt.Rows[row]["repository"].ToString();

                }

                 if (dt.Rows[row]["repository"].ToString() != repository)
                 {
                     if ((row > 0) || (row == dt.Rows.Count))
                     {
                         index = 1;
                         Consdata.Add("name:" + repository + "," + "region:" + sbu + "checkins:" + checkins.ToString() + "teamsize:" + teamsize.ToString() + "Checkintimes:" + checkintimes.ToString());
                         repository = dt.Rows[row]["repository"].ToString();
                         sbu = dt.Rows[row]["BusinessUnit"].ToString();
                         checkins.Add("[" + index + "," + dt.Rows[row]["AvgCheckinCount"].ToString() + "]");
                         checkintimes.Add("[" + index + "," + dt.Rows[row]["MeanBuildTimeHrs"].ToString() + "]");
                         teamsize.Add("[" + index + "," + dt.Rows[row]["TeamSize"].ToString() + "]");



                     }
                 }
                 else
                 {
                     if (row == 0)
                     {
                         index = 1;
                     }
                     else
                     {
                         index = index + 1;
                     }
                     repository = dt.Rows[row]["repository"].ToString();
                     sbu = dt.Rows[row]["BusinessUnit"].ToString();
                     checkins.Add("[" + index + "," + dt.Rows[row]["AvgCheckinCount"].ToString() + "]");
                     checkintimes.Add("[" + index + "," + dt.Rows[row]["MeanBuildTimeHrs"].ToString() + "]");
                     teamsize.Add("[" + index + "," + dt.Rows[row]["TeamSize"].ToString() + "]");
                 }

            }


My csv values are:
repository,modmonth,month,year,MeanBuildTimeHrs,AvgCheckinCount,TeamSize,BusinessUnit
aaa_aaaurf,1,6,2013,0,0,2,Financial & Risk - ALL
aaa_aaaurf,2,7,2013,0,0,2,Financial & Risk - ALL
aaa_aaaurf,3,8,2013,0,3,2,Financial & Risk - ALL
aaa_aaaurf,4,9,2013,0,0,2,Financial & Risk - ALL
aaa_aaaurf,5,10,2013,0,0,0,Financial & Risk - ALL
aaa_aaaurf,6,11,2013,0,0,0,Financial & Risk - ALL
aaa_aaaurf,7,12,2013,0,0,0,Financial & Risk - ALL
aaa_aaaurf,8,1,2014,0,0,0,Financial & Risk - ALL
aaa_aaaurf,9,2,2014,0,0,0,Financial & Risk - ALL
aaa_aaaurf,10,3,2014,0,0,0,Financial & Risk - ALL
aaa_aaaurf,11,4,2014,0,0,3,Financial & Risk - ALL
aaa_aaaurf,12,5,2014,62,2,3,Financial & Risk - ALL
aaa_ecso,1,6,2013,7,12,14,Financial & Risk - ALL
aaa_ecso,2,7,2013,6,37,10,Financial & Risk - ALL
aaa_ecso,3,8,2013,4,32,10,Financial & Risk - ALL
aaa_ecso,4,9,2013,4,57,10,Financial & Risk - ALL
aaa_ecso,5,10,2013,3,49,13,Financial & Risk - ALL
aaa_ecso,6,11,2013,2,62,13,Financial & Risk - ALL
aaa_ecso,7,12,2013,3,41,13,Financial & Risk - ALL
aaa_ecso,8,1,2014,2,51,16,Financial & Risk - ALL
aaa_ecso,9,2,2014,2,54,16,Financial & Risk - ALL
aaa_ecso,10,3,2014,2,56,16,Financial & Risk - ALL
aaa_ecso,11,4,2014,3,31,16,Financial & Risk - ALL
aaa_ecso,12,5,2014,0,38,16,Financial & Risk - ALL

1 个答案:

答案 0 :(得分:0)

你正在使用一个特殊的角色&#34; [&#34;。你应该使用&#34; \&#34;在角色之前。

例如:

checkins.Add("\\[" + index + ",\\"" + dt.Rows[row]["AvgCheckinCount"].ToString() + "\\]");
checkintimes.Add("\\[" + index + ",\\"" + dt.Rows[row]["MeanBuildTimeHrs"].ToString() + "\\]");
teamsize.Add("\\[" + index + ",\\"" + dt.Rows[row]["TeamSize"].ToString() + "\\]");`