This is complicated by the structure of the source data - the root object seems to combine static properties with keys for data rows.
The easiest way to work with this data will be to use
JSON.NET[
^]. You can add it as a NuGet package - search for either "JSON.NET" or "Newtonsoft.Json", which will both point to the same package:
JSON.NET | NuGet Gallery[
^]
(Don't be put off by the "Pricing" link in the Newtonsoft site header; that only applies to the other products on the site. JSON.NET is totally free, even if you're using it in a commercial application.)
The following classes will let you deserialize the data from the link you provided:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public sealed class Stats
{
public IDictionary<string, int> Total { get; set; }
public IDictionary<string, int> Visible { get; set; }
}
public sealed class RootObject
{
private IDictionary<string, JToken> _rawData;
private IDictionary<string, object[]> _data;
[JsonProperty("full_count")]
public int FullCount { get; set; }
public int Version { get; set; }
public Stats Stats { get; set; }
[JsonExtensionData]
private IDictionary<string, JToken> RawData
{
get
{
return _rawData;
}
set
{
_rawData = value;
_data = null;
}
}
[JsonIgnore]
public IDictionary<string, object[]> Data
{
get
{
if (_data == null)
{
if (_rawData == null || _rawData.Count == 0)
{
_data = new Dictionary<string, object[]>();
}
else
{
_data = _rawData.ToDictionary(pair => pair.Key, pair => pair.Value.ToObject<object[]>());
}
}
return _data;
}
}
}
As I said, this is more complicated than a normal JSON class. To get the dynamic properties deserialized into an array, we have to have a dictionary to contain any unknown properties, decorated with the
[JsonExtensionData]
attribute.
The values in that dictionary will be
JToken
objects - a class designed to represent dynamic JSON. Since we know the values will be arrays of objects, we convert the raw dictionary to one where the values are object arrays.
With these classes in place, you should then be able to parse the JSON data:
using (var client = new System.Net.WebClient())
{
string json = client.DownloadString(url);
RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
}