Parsing JSON data from Windows 8

This post is about parsing JSON data from Windows 8 clients, or from applications that support version 4.5 of the .Net framework.

Let’s begin with some imaginary JSON data.

JSON Data
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 [ { "" : "images/Chinese/Chinese_1_600_C.jpg",    "directions" : "Mix the ground pork with the salt, sugar, pepper, cornstarch and sesame oil. Add the dried mushrooms and Chinese sausage.Divide into 18 approximately equal portions. Take one portion of the meat and slightly flatten in your hand. Place a cooked quail egg on top of the meat and cover the meat around the egg to make a small round ball. Set aside. Dough:Mix the flour and sugar in a big bowl. Pour the milk in and mix until it becomes a dough Let it sit for 5 minutes. Divide into about 18 equal portions. If you feel any lumps, work through the dough with your hands to remove them. Forming and Cooking the Buns: flour, 18 pieces of dough, 18 balls of meat filling, parchment paper, cut into 18 square. On a floured surface, roll out a piece of dough into a circle, ideally tapered at the edges Dip one of the prepared meat fillings in flour, With the piece of dough in one hand, place the meat filling on top. ",    "" : [ { "image" : "images/Chinese/Chinese_1_600_C.jpg" },        { "image" : "images/Chinese/Chinese_2_600_C.jpg" },        { "image" : "images/Chinese/Chinese_3_600_C.jpg" }      ],    "favorite" : false,    "group" : { "backgroundImage" : "images/Chinese/chinese_group_detail.png",        "description" : "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis laoreet tempus libero vitae feugiat. Proin justo velit, luctus ornare posuere eget, pulvinar ac nibh. Integer dapibus leo in metus volutpat blandit. Morbi facilisis laoreet magna iaculis porta. Donec quis ipsum et justo faucibus lacinia. Vivamus id sapien nec dolor venenatis hendrerit. Aenean sit amet pretium quam. ",        "groupImage" : "images/Chinese/chinese_group.png",        "key" : "Chinese",        "rank" : "",        "recipesCount" : 6,        "shortTitle" : "Chinese",        "title" : "Chinese"      },    "ingredients" : [ "600 g Ground pork",        "1 tsp Salt",        "1 tsp sugar",        "1/2 tsp Ground black pepper",        "2 tsp Cornstarch",        "1 tbsp Sesame oil",        "1 handful Sliced dried mushrooms ",        "soaked and well drained",        "18 Quail eggs",        "cooked and peeled - alternately use 5 chicken eggs and cut them into quarters ",        "3/4 cup Sugar",        "4 1/2 cups Self-raising flour",        "1 1/2 cups Milk"      ],    "key" : 1000,    "preptime" : 60,    "rating" : 4,    "shortTitle" : "Steam bun baos",    "tileImage" : "images/Chinese/Chinese_1_150_C.jpg",    "title" : "Steam bun baos"  }]

I’m storing this as a resource that I’ve added to my Visual Studio project. The name of the resource file is Resources.resw.

You can easily add resource files to your Visual Studio project. Simply go to the Project menu and select add new item. From there navigate down and choose resources file.

magbvvo0
If I open up the resource editor, it looks like this: ggwrtanr


The Code behind Now that I’ve explained the data that we wish to parse, let’s look at some C sharp code to actually do the parsing. In the code below you can see that I’ve arbitrarily chosen two items inside the JSON data. I have chosen backgroundImage and extraImages. The code is fairly self-explanatory as seen below.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net.Http;using Windows.ApplicationModel.Resources;using Windows.Data.Json;using Windows.Foundation;using Windows.Foundation.Collections;using Windows.UI.Xaml;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Controls.Primitives;using Windows.UI.Xaml.Data;using Windows.UI.Xaml.Input;using Windows.UI.Xaml.Media;using Windows.UI.Xaml.Navigation;// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238namespace ReadJson{ /// /// An empty page that can be used on its own or navigated to within a Frame. /// public sealed partial class MainPage : Page { public MainPage() { this.InitializeComponent(); GetData(); } /// /// Invoked when this page is about to be displayed in a Frame. /// ///

The .NET framework provides excellent support for parsing JSON data. Notice the method calls of TryGetValue(), which essentially lets you “probe” to see if a particular data item exists. I hope this eliminates some of the challenges of parsing JSON data for developers.