C# Twitch Class 总是存储 null
C# Twitch Class always stores null
所以我是一个自学成才的 C# 程序员。我对 C# 的所有了解都来自于在学校学习 Java 并将其应用于 C#。我对 classes 的了解并不好,使用 JSON 数据几乎一无是处。所以我有这个 JSON 数据,我从 HttpWebClient 中拉下来,然后使用 JSON.NET 反序列化它。我为 class 结构使用了 json2csharp class 生成器。不管怎样,我相信错过了导致对象对所有内容都只有 null 的步骤。
示例JSON数据
https://github.com/justintv/Twitch-API/blob/master/v3_resources/games.md 获取 /games/top 示例数据
我的代码
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);
^这个应该有objectHolder.top[0].game.name;我只是不知道如何访问它。
抽搐 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; }
}
}
我确信我做错的是 class 和 Console 语句,我只是不太了解这样做。虽然我知道这很可能是非常简单的事情。
JSON 到 .NET 对象的反序列化只有在 .Net 实体与传入的 JSON 数据明显匹配时才有可能。在这里,您不必创建 Nested classes,而是必须创建 TwitchReturn class 并根据数据决定属性以及它是否是集合。
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; }
}
所以我是一个自学成才的 C# 程序员。我对 C# 的所有了解都来自于在学校学习 Java 并将其应用于 C#。我对 classes 的了解并不好,使用 JSON 数据几乎一无是处。所以我有这个 JSON 数据,我从 HttpWebClient 中拉下来,然后使用 JSON.NET 反序列化它。我为 class 结构使用了 json2csharp class 生成器。不管怎样,我相信错过了导致对象对所有内容都只有 null 的步骤。
示例JSON数据 https://github.com/justintv/Twitch-API/blob/master/v3_resources/games.md 获取 /games/top 示例数据
我的代码
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);
^这个应该有objectHolder.top[0].game.name;我只是不知道如何访问它。
抽搐 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; }
}
}
我确信我做错的是 class 和 Console 语句,我只是不太了解这样做。虽然我知道这很可能是非常简单的事情。
JSON 到 .NET 对象的反序列化只有在 .Net 实体与传入的 JSON 数据明显匹配时才有可能。在这里,您不必创建 Nested classes,而是必须创建 TwitchReturn class 并根据数据决定属性以及它是否是集合。
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; }
}