Parsing JSON In UWP App (Without JSON.NET) - Windows 10 Universal Apps

Well you would surely ask me, "Why without JSON.NET? ". Answer is really simple, its bad to have dependency of a library and you must know the other way around. When beta version of Windows 10 for developers was launched, I tried adding JSON.NET's old version and as a result I was not able to use it as a support for UWP apps since at that time it wasn't available. I just imagined that time,

"What if they don't provide support for Windows 10? ", "What if they end their support?". That made me move to native support of JSON parsing in Windows with help of,

using Windows.Data.Json;

It's a bit hard to parse JSON but according to me a Windows Developer should know both the ways to parse JSON.

  1. Using JSON.NET (Newtonsoft) Nuget package.
  2. Using Windows.Data.Json (Native Way).

Why is NewtonSoft (JSON.NET) so common when Windows Provide Native support for JSON parsing?

Well it started with Windows Phone (Silverlight) that didn't had native support for parsing JSON. Newtonsoft (JSON.NET) was one of the most efficient way to parse JSON on Windows Phone. More Windows Developers adopted it and used same way to parse JSON on Windows Phone, Windows Store and Web as well. It's easier to play with JSON. Then Microsoft introduced native support for parsing JSON which was Windows.Data.Json.

So for the time being just imagine there is no nuget package to parse JSON and you have to do it in a native way.

JSON.NET

Let's discover how we can parse JSON using Windows.Data,Json;

Whenever you are parsing JSON you need to request data using Client. Well it's same for parsing Json.net way or native way.

Code:

First you need initialize client to request JSON data,

  1. var client = new HttpClient();  
Then for obvious reasons you need to put request URL for JSON data,

Code
  1. HttpResponseMessage response = await client.GetAsync(new Uri("http://hello987.azurewebsites.net/androidJson.html"));  
It's time to get JsonString so that we can parse that,

Code
  1. var jsonString = await response.Content.ReadAsStringAsync();  
Now here things become a bit tricky, my json is consisted to simple JSONArray, your JSON may appear a bit different from what my json looks like. Here is a way to know how your json looks like. Just navigate to json2csharp.comput the url of JSON and it would return you with respective C# class of your JSON. You may use that to cast you JSON object.

As my JSON is a simple array so I just need to cast that to JSONArray and get out objects from that. I have saved my JSON respective C# class as "RootObject"

Code
  1. JsonArray root = JsonValue.Parse(jsonString).GetArray();  
  2. for (uint i = 0; i < root.Count; i++) {  
  3.     string name1 = root.GetObjectAt(i).GetNamedString("name");  
  4.     string description1 = root.GetObjectAt(i).GetNamedString("description");  
  5.     string link1 = root.GetObjectAt(i).GetNamedString("link");  
  6.     string cat1 = root.GetObjectAt(i).GetNamedString("cat");  
  7.     string image1 = root.GetObjectAt(i).GetNamedString("image");  
  8.     var chan = new RootObject {  
  9.         name = name1, cat = cat1, description = description1, image = image1, link = link1  
  10.     };  
  11.     obs_channels.Add(chan);  
  12. });  
Here obs_channels is my ObservableCollection where I am adding channels, whereas RootObject is my JSON respective C# class. Now you just need to bind your Observable Collection with UI. I would strongly suggest you to follow MVVM pattern and get full out of BLEND for VS.

tv chennel

This is a basic example of Parsing JSON in native way. Practice hard and it would make your parsing skills perfect.

Hope this post was helpful, see you next time with an amazing post.