I am using the following code to convert csv to json via C#:
var jsonWrtr = new StreamWriter(targetFile + ".json");
var csv = new List<string[]>(); // or, List<YourClass>
var lines = System.IO.File.ReadAllLines(targetFile + ".csv");
foreach (string line in lines) { csv.Add(line.Split(',')); }
string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(csv);
jsonWrtr.Write(json);
jsonWrtr.Close();
Here is an example of what it outputs:
[["Type","Id","Name","Age","Org","DOB","FirstName","LastName","Flight"],["1","2","3","4","5","6","7","8","9"],["1","2","3","4","5","6","7","8","9"]]
I would like the output in the following format:
[{
"Type" : "1",
"Id" : "2",
"Name" : "3",
"Age" : "4",
"Org" : "5",
"Dob" : "6", ,
"FirstName" : "7",
"LastName" : "8",
"Flight" : "9",
}, {
"Type" : "1",
"Id" : "2",
"Name" : "3",
"Age" : "4",
"Org" : "5",
"Dob" : "6", ,
"FirstName" : "7",
"LastName" : "8",
"Flight" : "9",
}
]
Any ideas why it's not working ?