Currently I have CSV along these lines :
"NAME","AGE","SEX"
"FRED, JONES","45","MALE"
"SALLY, SMITH","60","FEMALE"
And I use the following code to serialize it into JSON :
var linesCSV = System.IO.File.ReadAllLines(targetFile); //target file is the csv
var csv = linesCSV.Select(l => l.Split(',')).ToList();
var headers = csv[0];
var dicts = csv.Skip(1).Select(row => Enumerable.Zip(headers, row, System.Tuple.Create).ToDictionary(p => p.Item1, p => p.Item2)).ToArray();
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(dicts);
jsWrtr.WriteLine(json);
This gets outputted like so :
[{
"\NAME\"" : "\"FRED\"",
"\AGE\"" : "\"JONES\"",
"\SEX\"" : "\"45\""
},
{
"\NAME\"" : "\"SALLY\"",
"\AGE\"" : "\"SMITH\"",
"\SEX\"" : "\"60\""
}]
You can see the NAME
gets split up and the second part, part after the comma, gets put into the next field.
This is obviously because of the comma inbetween, but my question is how do I just parse the CSV so it outputs the following :
[{
"NAME" : "FRED, JONES",
"AGE" : "45",
"SEX" : "MALE"
},
{
"NAME" : "SALLY, SMITH",
"AGE" : "60",
"SEX" : "FEMALE"
}]