Cloud Zone is brought to you in partnership with:

My name is Bruno Terkaly and I am a Developer Evangelist in Northern California. Over the past few months I have been presenting a wide variety of demos and slides highlighting the wealth of new features in Visual Studio 2008. Bruno is a DZone MVB and is not an employee of DZone and has posted 43 posts at DZone. You can read more from them at their website. View Full User Profile

Parsing JSON by Hand from Azure Mobile Services

01.10.2013
| 2661 views |
  • submit to reddit


  1. This post is about parsing data by hand coming back from Azure Mobile Services.
    • It does not rely on the MobileServiceCollectionView to populate controls with data.
    • Use the code below if you want more control over the parsing of data.
Problem In a previous post, a friend of mine wanted to manually populate a Windows 8 GridView client using data coming back from Azure Mobile Services.

Based on this post http://blogs.msdn.com/b/brunoterkaly/archive/2012/06/15/how-to-provide-cloud-based-json-data-to-windows-8-metro-grid-applications-part-3.aspx#10382750
  1. Use this technique to manually parse JSON data coming back from Azure Mobile Services
    • It gives you fine grained control over the parsing of data coming back.
The goal You want to manually populate a gridview or similar control by hand. In other words, you want to parse JSON manually, one record at a time. Rather than getting an entire collection, you can use this technique to parse individual columns and rows.
Solution Use the JSONArray technique outlined below. Use the GetData() code below in the public MySampleDataGroup(JsonObject currGroup) constructor.
  1. You will need to edit the code as you see below.
    • There is the application key
    • There is the DNS name
  2. Both of these items will differ with your own version of Azure Mobile Services
rZYYXzOAKgiukahLDniLPeydiMpefy22 You get this from the Azure Mobile Services Portal. It is the application key.
https://brunotodoservice.azure-mobile.net/tables/TodoItem You get this from the Azure Mobile Services Portal. It is the DNS name you get when you create the service.
text A column in your SQL Server Table.

The Code to parse data coming back from Azure Mobile Services

public async void GetData()
{
    // Part of the namespace "System.Net.Http"
    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Add("X-ZUMO-APPLICATION", "rZYYXzOAKgiukahLDniLPeydiMpefy22");
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    //
    // Asynchronously call into the web service
    //
    var response = await client.GetAsync(
        new Uri("https://brunotodoservice.azure-mobile.net/tables/TodoItem"));

    //
    // Read the data as a big string
    //
    var result = await response.Content.ReadAsStringAsync();

    //
    // Parse the JSON data
    //
    var parsedResponse = JsonArray.Parse(result);

    //
    // Convert to a JSON array
    //
    JsonArray array = parsedResponse;

    IJsonValue outValue;
    foreach (var item in array)
    {
        var obj = item.GetObject();
        // Extract the text key. Assume there is a “text” column coming back
        if (obj.TryGetValue("text", out outValue))
        {
            string textValue = outValue.GetString();

        }
    }
}


  1. For Andrew:
    1. The best way to learn all the details of using Azure Mobile Services is to do a lab in the Windows Azure Training Kit.

Windows Azure Training Kit - December 2012 http://www.microsoft.com/en-us/download/details.aspx?id=8396
It will install in the c:\watk folder. Go to this link once you have installed it. C:\WATK\Labs\Windows8AndMobileServices\HOL.htm

Published at DZone with permission of Bruno Terkaly, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)