How to parse/deserialize a JSON string in C# XAML Windows 8 Metro App? How to parse/deserialize a JSON string in C# XAML Windows 8 Metro App? json json

How to parse/deserialize a JSON string in C# XAML Windows 8 Metro App?


The official JSON APIs for Windows Store Apps are in the Windows.Data.Json namespace:

  • JsonObject.Parse() or new JsonOject() for objects, it works more less like a Dictionary<TKey, TValue>.
  • JsonArray.Parse() or new JsonArray() for arrays, it work more less like a List<TValue>.
  • JsonValue.Parse(), JsonValue.CreateStringValue(), JsonValue.CreateBooleanValue() or JsonValue.CreateNumberValue() for string, boolean, number and null values.

Check some samples here: http://msdn.microsoft.com/en-us/library/windows/apps/hh770289.aspx

You won't need to add any library.


If you have used Json.NET in other .NET profile, you can add the library to your Windows Store app project via NuGet.

Here are some examples:

  1. Object to Json

    var obj = new { Name = "Programming F#", Author = "Chris Smith" };

    string json = JsonConvert.SerializeObject(obj, Formatting.Indented);

  2. Querying Json

    var json = @"{""Name"": ""Programming F#"",""Author"": ""Chris Smith""}";

    var jObject = JObject.Parse(json);

    string name = (string)jObject["Name"]; // Programming F#

  3. Json to Array

    string json = @"['F#', 'Erlang', 'C#', 'Haskell', 'Prolog']";

    JArray array = JArray.Parse(json);

    foreach (var item in array){ string name = (string)item;}

You can find Json.NET documentation here.


When consuming JSON REST services I've found by far the best way to deserialise the JSON is to use the HttpContentExtensions class which contains ReadAsASync(HTTP Content) alongside HttpClient. This extension class can be found by installing the Microsoft ASP.NET Web API 2.2 Client NUGET package.

Making a web request and deserialising is then just this simple:

private const string baseUri = "https://weu.google.co/";private HttpClient client = new HttpClient();var result = await client.GetAsync([Your URI]);var data  = await result.Content.ReadAsAsync<YourClass>();return data.Value;