Using JsonConvert.DeserializeObject on Nested Object
<p>I want to use JSON.Net to handle parsing a configuration file when my application is loaded. Keeping all KVP's in the same scope works absolutely fine. I would however like to break it down into sub-categories such as Settings.WebServer, Settings.GameServer, etc.</p>

<p>I would like to be able to reference various settings in that manner for readability sake, such as Settings.WebServer.hostname. Currently trying to factor in WebServer/GameServer is throwing this off. Can anyone help with what can be done to get this working?</p>

<p>JSON</p>

<pre><code>{
    "webserver":
    {
        "hostname": "localhost",
        "port": "8888"
    },
    "gameserver":
    {
        "hostname": "123.123.123.123",
        "port": "27015",
        "password": "as@c!qi$"
    }
}
</code></pre>

<p>C# Main</p>

<pre><code>Settings settings = JsonConvert.DeserializeObject&lt;Settings&gt;(File.ReadAllText(@".\Configs\settings.cfg"));
</code></pre>

<p>C# Settings Class</p>

<pre><code>namespace SourceMonitor
{
    public class Settings
    {
        public class Webserver
        {
            [JsonProperty("hostname")]
            public string hostname { get; set; }

            [JsonProperty("port")]
            public string port { get; set; }
        }

        public class Gameserver
        {
            [JsonProperty("hostname")]
            public string hostname { get; set; }

            [JsonProperty("port")]
            public string port { get; set; }

            [JsonProperty("password")]
            public string password { get; set; }
        }

        public class RootObject
        {
            [JsonProperty("webserver")]
            public Webserver webserver { get; set; }

            [JsonProperty("gameserver")]
            public Gameserver gameserver { get; set; }
        }
    }
}
</code></pre>