I am developing a C#/XAML metro application in which I comsume a JSON REST Services. How can I deserialize a JSON response into a text or a custom object?

- 3,060
- 6
- 34
- 62

- 973
- 1
- 20
- 42
-
Have a look at [this](http://stackoverflow.com/questions/13605667/c-sharp-json-parsing) – Armand Sep 30 '13 at 12:30
3 Answers
The official JSON APIs for Windows Store Apps are in the Windows.Data.Json
namespace:
JsonObject.Parse()
ornew JsonOject()
for objects, it works more less like aDictionary<TKey, TValue>
.JsonArray.Parse()
ornew JsonArray()
for arrays, it work more less like aList<TValue>
.JsonValue.Parse()
,JsonValue.CreateStringValue()
,JsonValue.CreateBooleanValue()
orJsonValue.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.

- 15,852
- 13
- 78
- 101
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:
Object to Json
var obj = new { Name = "Programming F#", Author = "Chris Smith" };
string json = JsonConvert.SerializeObject(obj, Formatting.Indented);
Querying Json
var json = @"{""Name"": ""Programming F#"",""Author"": ""Chris Smith""}";
var jObject = JObject.Parse(json);
string name = (string)jObject["Name"]; // Programming F#
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.

- 2,221
- 22
- 23
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;

- 4,180
- 6
- 35
- 58