I tried to convert CSV data to JSON. It quiet worked fine but few columns have comma, while converting to json, comma contained data is getting split. This is the code I tried,
var path = @"C:xyz\\abc.csv";
var csv = new List<string[]>();
var lines = File.ReadAllLines(path);
foreach (string line in lines)
csv.Add(line.Split(','));
var properties = lines[0].Split(',');
var listObjResult = new List<Dictionary<string, string>>();
for (int i = 1; i < lines.Length; i++)
{
var objResult = new Dictionary<string, string>();
for (int j = 0; j < properties.Length; j++)
objResult.Add(properties[j], csv[i][j]);
listObjResult.Add(objResult);
}
var json = JsonConvert.SerializeObject(listObjResult, Formatting.Indented);
List<ABCModel> desrilize = JsonConvert.DeserializeObject<List<ABCModel>>(json);
return desrilize;
CSV data
employee,emp city,state,emp address
"abc","efg","lkj","building name"
"wer","sdf","qwe","afj Building, near cross"
In above third line contains comma, which should not get split while converting to json. Where as using above code, its getting split. Kindly help.
Also there is a space in "emp city", how to define jsonProperty for the same while creating model.
Expected json
[
{
"employee": "abc",
"emp city": "efg",
"state": "lkj",
"emp address": "building name"
},
{
"employee": "wer",
"emp city": "sdf",
"state": "qwe",
"emp address": "afj Building, near cross"
}
]