I'm using Newtonsoft JSON in C#.
I have a log file which I am trying to parse so I can populate a database with the data in the log file.
In the log file, each line is a separate JSON object.
Here is what the log file looks like:
{ "timestamp":"2020-02-02T04:49:53Z", "event":"Friends", "Status":"Online", "Name":"User1" }
{ "timestamp":"2020-02-02T04:48:06Z", "event":"Commander", "FID":"F2", "Name":"User2" }
{ "timestamp":"2020-02-02T04:48:06Z", "event":"Materials", "Raw":[ { "Name":"cadmium", "Count":9},
{ "Name":"zinc", "Count":45 }, { "Name":"iron", "Count":71 }]}
Here's what I'm doing so far:
var fullPath = @"X:\Data\Log Files\test log.log";
string[] lines = File.ReadAllLines(fullPath);
foreach (var line in lines)
{
var json = JObject.Parse(line);
var eventType = json["event"].Value<string>();
JArray raw = (JArray)line["Raw"];
switch (eventType)
{
case "Friends":
var status = json["Status"].Value<string>();
var frName = json["Name"].Value<string>();
Console.WriteLine("The friend " + frName + " is currently " + status + ".");
Console.WriteLine(json);
break;
case "Commander":
var fid = json["FID"].Value<string>();
var CMDR = json["Name"].Value<string>();
Console.WriteLine("User " + CMDR + " with ID " + ".");
Console.WriteLine(json);
break;
case "Materials":
//do something here that is magical.
break;
default:
Console.WriteLine("N/A");
break;
}
The first two cases work fine. However, I can't figure out how to get the array "Raw" out of the "Materials" event.
FYI - there are two other arrays in that line. So, I need to be able to extract the array for "Raw" as well as the others.
So far, I just can't figure this out.
I'm very much a beginner, so please explain this as if you were talking to a toddler. :)
please help with this?
Thanks so much!