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
-
-
-
-
- public string id { get; set; } = string.Empty;
-
-
-
-
- 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.
- ...
-
- public class MenuObj
- {
- #region Properties
-
-
-
-
- public string header { get; set; } = string.Empty;
-
-
-
-
- public List<ItemObj> items { get; set; } = new List<ItemObj>();
-
- #endregion
- }
-
- ...
In the above code, you can see that the menu
object class contains the properties according to the sample JSON with
the names of the properties which are exactly according to the JSON
structure with case sensitivity.
Step 7
For the
final object, we need to create the main object which holds the complete
structure of the JSON as according to our sample JSON file, there is
still one object (the main object) whose object class with provided name
has not yet been created, so, create "Objects\JSONMapperObj.cs" file and replace it with following code i.e.
- ...
-
- public class JSONMapperObj
- {
- #region Properties
-
-
-
-
- public MenuObj menu { get; set; } = new MenuObj();
-
- #endregion
- }
-
- ...
From the above, you can verify that the most
top object mentioned in our sample JSON is "menu" which holds the entire
JSON objects, so, I have created the main object according to the
sample JSON with the names of the properties which are exactly
according to the JSON structure with case sensitivity.
Step 8
Now, open "Program.cs" file and add following lines of code i.e.
- ...
-
-
- JSONMapperObj jsonObj = new JSONMapperObj();
-
-
- ...
-
- string data = JsonConvert.SerializeObject(jsonObj);
- ...
-
- JSONMapperObj targetData = JsonConvert.DeserializeObject<JSONMapperObj>(data);
-
- ...
The above code simply prepare the sample JSON
data according to the sample JSON, the serialize the JSON object to pass
the data over either web API or on network, then target data is
deserialized and can be manipulated easily.
Step 9
Now, execute the project and you will be able to see following output i.e.
You can validate your JSON string using online JSON validator tool like
JSONLint or any of your choice.
Conclusion
In this article, you learned to create a complex
JSON object mapper using C#.NET technology Console Application. You also learned to break down your target JSON and then create required JSON
mapper objects with the names of the properties exactly according to the
JSON structure with case sensitivity. You learned to serialize and
deserialize the JSON string & object to pass it over the network or to share it via web API. Finally, you learned about the tools you need to validate
your JSON data.