1

I am writing a JsonParser in C# (using a reference from SO). This might be a simple question, but I am new to C#, and thanks for your help.

I am wondering if a JObject can be passed to another function for JSON parsing.

using (StreamReader r = new StreamReader("Input.json"))
{
    string json = r.ReadToEnd();
    dynamic array = JsonConvert.DeserializeObject(json);
    foreach(var item in array)
    {
        Console.WriteLine("Item Type: {0}", item.GetType());
        Console.WriteLine("ID: {0}", item.ID);
        Console.WriteLine("Name: {0}", item.Name);
        Console.WriteLine("Age: {0}", item.Age);
    }
}

I want to do the JSON parsing in a dynamic approach without creating a POJO. Can I pass the item to a function that takes care of the parsing? What will the object type be that I could pass here?

static void parseJson() {

    using (StreamReader r = new StreamReader("Input.json"))
    {
        string json = r.ReadToEnd();
        dynamic array = JsonConvert.DeserializeObject(json);
        foreach(var item in array)
        {
            Console.WriteLine("Item Type: {0}", item.GetType());
            parseItem(item);
        }
    }
}

static void parseItem(JObject item) {
    Console.WriteLine("ID: {0}", item.ID);
    Console.WriteLine("Name: {0}", item.Name);
    Console.WriteLine("Age: {0}", item.Age);
}

Say my Json file is something like this.

[
  {
     "ID": "1",
     "Name": "abc"
     "Age": "1"
     "Emails": [
      "abc@def.com",
      "abc123@def.com",
      "abc@ghi.com",
    ]
  }
]
SyncMaster
  • 9,754
  • 34
  • 94
  • 137
  • Your code can not be even compilled parseItem(JObject item) { Console.WriteLine("ID: {0}", item.ID); – Serge May 02 '23 at 16:21
  • This is a snippet of code I shared to show my high-level idea or intention. I want to be able to pass each object I get from Json parsing to a separate function where I can print it. – SyncMaster May 02 '23 at 16:47

2 Answers2

0

If you want to use JObjects, you should use JObject result = JObject.Parse(json) instead of dynamic array = JsonConvert.DeserializeObject(json);

To access the properties of that JObject you would use the syntax var id = result['ID']

Ethan Fischer
  • 1,229
  • 2
  • 18
  • 30
0

you can try this code, if your json object has not nested properties and all properties are primitive

static void parseItem(string json)
{
    var item = (JObject)JArray.Parse(json)[0];
    
    foreach (JProperty prop in item.Properties())
    {
        if (prop.Value.Type == JTokenType.Array)
            Console.WriteLine($"{prop.Name}:  {string.Join(",  ", ((JArray)prop.Value))}");
        else Console.WriteLine($"{prop.Name}: {prop.Value}"); ;
    }
}

or if your properties are more complicated

    var jt = JToken.Parse(json);
    parseItem(jt);

static void parseItem(JToken jt)
{
    var item = (JObject)jt[0];
    var emails = item["Emails"].ToObject<string[]>();
}
Serge
  • 40,935
  • 4
  • 18
  • 45
  • My question is, can the `prop` be passed to another function? If so what will be the type of `prop`? Say I want a separate function to take care of printing the `prop` to the console. – SyncMaster May 02 '23 at 16:46