David Smith

David Smith

  • NA
  • 2k
  • 0

Json Parser Logic Help

Jul 17 2016 3:48 PM

Is there a better way to parse this information below with exception handling. I am only interested in data in between curly { ....... }. But I need to capture curly brackets only with data so I can send it through the JObject parser.

This is my current logic below and also review the logic I am parsing.

public List<JObject> ReadJsonFile(string fileName)
{
JObject jsonData = null;

using (StreamReader file = File.OpenText(fileName))
{
string line = file.ReadLine();

while ((line = file.ReadLine()) != null)
{
string [] jsonLine = line.Split('{');

if (jsonLine.Count() == 2)
{
string json = "{" + jsonLine[1];

jsonData = JObject.Parse(json);

JObjectList.Add(jsonData);
}
}
}

return null;
}

File name is dummydata.txt:

20160705-150501, Food, Starting application.
20160705-150502, Food, Food Prep initialization complete.
20160705-150503, Food,Starting Food IoT communications...
20160705-150504, Food,Attempting to connect to Food Iot Servers...
20160705-150505, Food,Client connected.
20160715-174706,Topic Uplink: Food/FoodItem12/uplink/command
20160705-150507,Food_Recieved,{"Start":null,"Timestamp":"2016-03-05T00:15:07Z","Carbs": brown rice,"Veggies":Celery,"Fruits":Orange,"Fats":Almonds,"End":null}
20160705-150508,Food_Recieved,{"Start":null,"Timestamp":"2016-03-05T00:15:08Z","Carbs": brown rice,"Veggies":Celery,"Fruits":Orange,"Fats":Almonds,"End":null}
20160705-150509, Food,Client disconnected
20160705-150510, Food, Starting application.
20160705-150511, Food, Food Prep initialization complete.
20160705-150512, Food,Starting Food IoT communications...
20160705-150513, Food,Attempting to connect to Food Iot Servers...
20160705-150515, Food,Client connected.
20160715-174715,Topic Uplink: Food/FoodItem13/uplink/command
20160705-150516,Food_Recieved,{"Start":null,"Timestamp":"2016-03-05T00:15:16Z","Carbs": yams,"Veggies":brocolli,"Fruits":Strawberry,"Fats":Pecans,"End":null}
20160705-150517,Food_Recieved,{"Start":null,"Timestamp":"2016-03-05T00:15:15Z","Carbs": banana,"Veggies":carots,"Fruits":Apple,"Fats":Peanuts,"End":null}



pianoboyCoder



Answers (2)