title: How to deserialize JSON to objects of the correct type, without having to define the type before hand?
body: <p>I searched through similar questions and couldn't find anything that quite matched what i was looking for.</p>
  
  <p>New to C# so bear with me please.</p>
  
  <p>I have some json files that i am deserializing. I want the files to deserialize to objects of the correct type, without having to define the type before hand. Here's my code:</p>
  
  <pre><code>public class loadJson
      {
          //path of the file location
          public void readJson(string path)
          {
              //array of files at the path location. right now just reading one file
              FileInfo[] files = new DirectoryInfo(path).GetFiles("seleniumExample.json").ToArray();
  
              foreach (FileInfo fi in files)
              {
  
                  dynamic b1 = null;
                  using (StreamReader file = new StreamReader(fi.FullName))
                  {
  
                      string fileText = file.ReadToEnd();
                      //Console.WriteLine(fileText);
  
                      try
                      {
                          b1 = Newtonsoft.Json.JsonConvert.DeserializeObject(fileText);
                      }
                      catch(Exception e)
                      {
                          Console.WriteLine("ERROR!!!! " + e.ToString());
                      }
  
                      file.Close();
                  }
              }
          }
      }
  </code></pre>
  
  <p>I have a bunch of object types that I will be feeding into my program through json files. </p>
  
  <p>I don't want to have to explicitly call b1 a Bid, or a Client, or any other specific predefined class. If I do explicitly call b1 a Bid, it loads all the info just fine and fills out the correct instance variables.</p>
  
  <p>But when I use "dynamic", or general "object", it can't figure it out and just initializes to an "object".</p>
  
  <p>Is there a way to perform generic deserialization and have it create an object of the correct class based on the fields defined in the json file?</p>
  
  <p>Thanks in advance for the help, and i apologize if my question is incredibly unclear. If so, please just let me know how I can help clear up any ambiguity. Thanks again. </p>