C#Twitch类总是存储null



所以我是一个自学成才的C#程序员。关于C#,我所知道的一切都来自于在学校学习Java并将其应用于C#。我对类的了解并不出色,使用JSON数据几乎完全不是什么。所以我有这个JSON数据,我从HttpWebClient中提取,然后使用JSON.NET对其进行反序列化。我使用了json2csharp类生成器作为类结构。不管我是谁,我认为我错过了一个步骤,那就是导致对象对所有内容都只有null。

JSON数据示例https://github.com/justintv/Twitch-API/blob/master/v3_resources/games.mdGET/游戏/顶级示例数据

我的代码

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;我只是不知道如何访问这个。

Twitch退货类

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; }
    }
}

我确信我做错的事情来自于那个类和Console语句,我只是不知道该怎么做。虽然我知道这很可能是非常简单的事情。

JSON到.NET对象的反序列化只有在.NET实体与传入的JSON数据明确匹配时才可能实现。在这里,您必须创建TwitchReturn类,而不是创建Nested类,并根据数据决定属性,以及它是否是集合。

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; }
}

最新更新