如何在c#中添加类似JSON格式的逗号分隔值
本文关键字:格式 分隔 JSON 添加 | 更新日期: 2023-09-27 18:14:32
我试图通过读取csv并填充数据表来创建类似于JSON兼容性的输出。然后循环创建一个指定的格式(如下所述)。
我要找的是添加逗号分隔值内的开括号和闭括号如下格式指定的签入,团队大小和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
您正在使用特殊字符"["。你应该在字符前使用"'"。
例如:checkins.Add("''[" + index + ",''"" + dt.Rows[row]["AvgCheckinCount"].ToString() + "'']");
checkintimes.Add("''[" + index + ",''"" + dt.Rows[row]["MeanBuildTimeHrs"].ToString() + "'']");
teamsize.Add("''[" + index + ",''"" + dt.Rows[row]["TeamSize"].ToString() + "'']");`