How can I loop through child nodes using Json.NET?
<p>I'm trying to loop through some JSON data and print it to the screen. Here's what the JSON looks like:</p>

<p><a href="https://i.stack.imgur.com/1xHJp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1xHJp.png" alt="JSON data"></a></p>

<p>Using this code, I'm able to loop through the list of category names without any issues:</p>

<pre><code>dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

foreach (var obj in jsonObj.categories)
{
    &lt;p&gt; &lt;strong&gt; @obj.name &lt;/strong&gt; &lt;/p&gt;
}
</code></pre>

<p>I can't figure out how to loop through subcategories though. I tried this, but I just get Error - Object reference not set to an instance of an object.</p>

<pre><code>foreach (var obj in jsonObj.categories)
{
    &lt;p&gt; &lt;strong&gt; @obj.name &lt;/strong&gt; &lt;/p&gt;

    foreach (var subcat in jsonObj.subcategories)
    {
        &lt;p&gt; &lt;strong&gt; @obj.name &lt;/strong&gt; &lt;/p&gt;
    }
}
</code></pre>