JSON (JavaScript Object Notation) is one of the most commonly used
data passing formats used in Web API(s) development. Parsing &
manipulating complex JSON objects becomes quite the headache especially
with array parsing technique. JSON objects parsing using C#.NET is on
the other hand is quit simple, all you have to do is to create
your target JSON object mapper and then deserialize the JSON
string/object.
Today, I shall be demonstrating creation of a complex JSON object mapper using C#.NET technology Console Application.

Prerequisites
Following are some prerequisites before you proceed any further in this tutorial,
- Knowledge of JSON string.
- Knowledge of C# Programming.
The example code is
being developed in Microsoft Visual Studio 2019 Professional.
Let's begin now.
Step 1
Create a new C#.NET Console Application project and name it "JSONMapper".
Step 2
Open "Tools\Nuget Package Manage\Manage Nuget Packages for Solution...".
Step 3
Install Newtonsoft.Json NuGet Package i.e.

Step 4
I am using the following sample JSON data i.e.

Let's breakdown this sample JSON in order to create out Object mapper.

In
the above snippet, 1, 2, 5, & 6 points indicate the start & end
of the JSON object and any list object inside. Point 4 indicates names
of the object which should be used exactly as they are mentioned that
includes case sensitivity as well, these names will be used during the
creation of our JSON mapper object. Point 3 indicates start of the
object and point 7 indicates value of the object which could be a list,
integer value, bool, string etc. Point 8 highlights a single item of the
list object which needs to be constructed according to the structure
provided in point 8.
Step 5
So, based on the above understanding of our sample JSON, create list item structure first, by creating a new "Objects\ItemObj.cs" file and replace following code in it i.e.
- ...
- public class ItemObj
- {
- #region Properties
- /// <summary>
- /// Gets or sets ID property.
- /// </summary>
- public string id { get; set; } = string.Empty;
- /// <summary>
- /// Gets or sets label property.
- /// </summary>
- public string label { get; set; } = string.Empty;
- #endregion
- }
- ...
In the above code, I have created the list
item object structure according to my sample JSON, notice that the names
of the properties are exactly according to the JSON structure with case
sensitivity.
Step 6
Now, create a menu object class
which contains a string object and a list object of type ItemObj that I
have created in the previous step, so, create a new "Objects\MenuObj.cs" file and replace following code in it i.e.


David KurniawanPosted Feb 18, 2022, 11:23 AM
Good to start from here. thanks
DennisPosted Dec 15, 2021, 8:45 PM
I don't see how the data is being inserted into the json object.
Dennis TaylorPosted Aug 31, 2020, 3:14 PM
She wants you to buy the source code for this. Really?
Divya RaniPosted Aug 16, 2020, 8:56 PM
No source code available with this article. :(
Divya RaniPosted Aug 16, 2020, 8:56 PM
There is no source code :(
Tomasz GruźlaPosted Jan 2, 2020, 11:40 AM
Step 8... data.ToString() ???? why string.ToString() ??
cherkaoui mouadPosted Jan 2, 2020, 10:16 AM
AWESOME :)
Asma KhalidPosted Jan 2, 2020, 9:43 AM
Thank you all for your support and kind words.
mohd arifPosted Jan 2, 2020, 6:01 AM
Nice one and Well Explained
Sourav Kumar DasPosted Jan 1, 2020, 10:43 PM
Nice and useful article Mam.