So I am a self taught C# programmer. Everything I know about C# is from studying Java in school and applying it to C#. My knowledge of classes is not excellent and using JSON data is almost completely nothing. So I have this JSON data that I am pulling down from a HttpWebClient and then deserializing it using JSON.NET. I used json2csharp class generator for the class structure. However I am, I beleive missing one step that is causing the object to only have null for everything.
Example JSON Data https://github.com/justintv/Twitch-API/blob/master/v3_resources/games.md GET /games/top example data
My code
var client = new HttpClient();
var url = new Uri("https://api.twitch.tv/kraken/games/top");
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/vnd.twitchtv.v3+json"));
var response = await client.GetAsync(url);
var content = await response.Content.ReadAsStringAsync();
TwitchReturn objectHolder = JsonConvert.DeserializeObject<TwitchReturn>(content);
Console.WriteLine(objectHolder);
^This should have objectHolder.top[0].game.name; I just don't know how to access this.
Twitch Return Class
class TwitchReturn
{
public class Links
{
public string self { get; set; }
public string next { get; set; }
}
public class Box
{
public string large { get; set; }
public string medium { get; set; }
public string small { get; set; }
public string template { get; set; }
}
public class Logo
{
public string large { get; set; }
public string medium { get; set; }
public string small { get; set; }
public string template { get; set; }
}
public class Links2
{
}
public class Game
{
public string name { get; set; }
public int _id { get; set; }
public int giantbomb_id { get; set; }
public Box box { get; set; }
public Logo logo { get; set; }
public Links2 _links { get; set; }
}
public class Top
{
public int viewers { get; set; }
public int channels { get; set; }
public Game game { get; set; }
}
public class RootObject
{
public int _total { get; set; }
public Links _links { get; set; }
public List<Top> top { get; set; }
}
}
I am sure that what I am doing wrong comes from that class and the Console statement, I just don't know enough to do that. Although I know it is most likely something very simple.
JSON to .NET object deserialization is only possible when .Net Entity clearly matches with the incoming JSON data. Here, instead of creating Nested classes, you would have to create TwitchReturn class and based on the data decide on the properties and also, whether it will be collection or not.
class TwitchReturn
{
public Links _links { get; set; }
public IEnumerable<Top> top { get; set; }
public RootObject rootObject { get; set; }
}
public class Links
{
public string self { get; set; }
public string next { get; set; }
}
public class Box
{
public string large { get; set; }
public string medium { get; set; }
public string small { get; set; }
public string template { get; set; }
}
public class Logo
{
public string large { get; set; }
public string medium { get; set; }
public string small { get; set; }
public string template { get; set; }
}
public class Links2
{
}
public class Game
{
public string name { get; set; }
public int _id { get; set; }
public int giantbomb_id { get; set; }
public Box box { get; set; }
public Logo logo { get; set; }
public Links2 _links { get; set; }
}
public class Top
{
public int viewers { get; set; }
public int channels { get; set; }
public Game game { get; set; }
}
public class RootObject
{
public int _total { get; set; }
public Links _links { get; set; }
public List<Top> top { get; set; }
}