A quick way to explore this structure and get started with json in C# is to copy the json packet as you expect it to be. Taking what you have in your question as an example, copy the json into the clipboard and create a new C# code file and write your default namespace only as follows:
namespace Data
{
// Next step will be here
}
After that, place your cursor between the curly braces, click the edit menu, then paste special, JSON as Classes
This will generate some classes for you that match the JSON schema you have. In this case, there's a Class1 because it cannot determine the name of the object with the toplevel
, secondlevel
etc properties. You can give it a more meaningful name for your case.
Update for .NET 5.0 and later versions
Following the release of .NET 5.0, a new namespace now comes out of the box that can handle JSON as easily as Newtonsoft and in a performant way.
The simplest way to parse the Json into your objects will be with a one liner as follows...
var items = JsonSerializer.Deserialize<IEnumerable<MyItem>>(jsonData);
For this to work, you'll need to have the following in your usings...
using System.Collections.Generic; // For IEnumerable<T>
using System.Text.Json; // For JsonSerializer.*
Following is the original answer that used the Newtonsoft.Json Nuget package. That still works with .NET 5.0 but I've included the new namespace for those who would wish to avoid including an extra DLL that has functionality that is covered out of the box.
Original method using Newtonsoft.Json
Open the package manager console (if you can't see it in VS, press Ctrl+Q and type "Package Manager Console"). In the package manager, type the following
Install-Package Newtonsoft.Json
after ensuring that you have your solution saved and specifying the project you want to add that package to as your default project as in the screenshot:

and wait for it to complete. You'll now have the ability to deserialize JSON you have into your classes. Go to where you want to do the processing and type the following...
// at the top of the file...
using Newtonsoft.Json;
// where you need to decode it...
string jsonData = GetYourJsonDataFromAFileOrAPICall(); // replace with how you get the json
var items = JsonConvert.DeserializeObject<IEnumerable<MyItem>>(jsonData);
At this point, you'll have an IEnumerable<MyItem>
(MyItem
is what I renamed Class1
) representing your json. You can change that to be a list, or array if you want a more specific collection.