/// <summary>
/// CSVToJSON
/// </summary>
/// <param name="filePath">CSV文件路径</param>
public void CsvToJson(string filePath)
{
JObject sta = new JObject(); //新建一个JObject对象
sta.Add(new JProperty("starttime", "20180101"));
JArray arr = new JArray(); //新建一个JArray对象
Dictionary<string, int> lstTrueField = new Dictionary<string, int>(); //新建键值对对象
StreamReader reader = new StreamReader(filePath, System.Text.Encoding.Default, false); //读取CSV文件
int m = 0;
while (!reader.EndOfStream)
{
m = m + 1;
string str = reader.ReadLine();
string[] split = str.Split(',');
if (m == 1) //第一行作为列名
{
for (int c = 3; c < split.Length; c++)
{
lstTrueField.Add(split[c], c); //课程与列序号
}
}
if (m > 1)
{
arr.Add(new JObject(new JProperty("Name", split[0]), new JProperty("Age", split[1]),new JProperty("Sex",split[2]),new JProperty("course", newJArray(new JObject(from p in lstTrueField select new JProperty(p.Key,split[p.Value]))))));
}
}
reader.Close(); //关闭流
sta.Add(new JProperty("locdata", arr)); //将JArray数组加入JObject对象内。
}