I'm trying to loop through some JSON data and print it to the screen. Here's what the JSON looks like:

JSON data

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

dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(json);

foreach (var obj in jsonObj.categories)
{
    <p> <strong> @obj.name </strong> </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.

foreach (var obj in jsonObj.categories)
{
    <p> <strong> @obj.name </strong> </p>

    foreach (var subcat in jsonObj.subcategories)
    {
        <p> <strong> @obj.name </strong> </p>
    }
}

Related posts

Recent Viewed