Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using c# parse and iterate through json object to address each field

Tags:

json

c#

This is a very simple question but can't seem to find a direct answer. I read in a single JSON object. I then want to parse it and be able to directly address a token or a value and then format it for writing a file output, which I will use in another application. I am using C# and the Newtonsoft library.

My code:

JsonTextReader reader = new JsonTextReader(re);
while (reader.Read())
{
    if (reader.Value != null)
    Console.WriteLine("Value: {0}", "This is the value <Tags>:  " + reader.Value);
}

How can I address each line? for example, desc and then Gets a reference to the game world. This must be so commoplace.

Thanks,

johnh

like image 539
johnh Avatar asked Mar 23 '23 17:03

johnh


1 Answers

Use the JArray and JObject objects instead, like this:

var json = System.IO.File.ReadAllText("YourJSONFilePath");
var objects = JArray.Parse(json);

foreach(JObject root in objects)
{
    foreach(KeyValuePair<String, JToken> tag in root)
    {
        var tagName = tag.Key;
        Console.WriteLine("Value: {0}", "This is the value <Tags>:  " + tagName);
    }
}
like image 85
Karl Anderson Avatar answered Apr 06 '23 03:04

Karl Anderson