I've written a c# application that reads JSON files line by line and write csv files from it. I've create Model files for each csv format, objects for those model gets instantiated while parsing and are then written to csv at the end.
For Ex: if input file name is abc.json, create and instantiate object for abc, store it in data structure like List and then write it to csv at the end.
JSON file:
{
"Computer ID": "1697343078",
"Application Name": "Reporting Services Service",
"Date": "8\/25\/2015",
"Count": "1"
}
My code to parse is as follows:
using (System.IO.StreamReader sr = new System.IO.StreamReader(sFile, Encoding.UTF8))
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("Computer ID") && counter == 4)
{
string[] tokens = line.Split(':');
if (tokens.Length >= 2)
{
resourceID = reg.Replace(tokens[1], "");
}
counter = counter - 1;
line = sr.ReadLine();
}
}
The parsing fails because of inconsistent format of data or other fields in input file. Code throws exception and parsing of that particular file fails completely. I want my code to reject the record for which parsing and continue parsing other records in the file and to finally generate a csv for it.
I want it to behave as below, Read the file line by line If any error occurs while parsing, don't instantiate that object and continue parsing other lines for that file Write the object to csv
Any help would be appreciated.