所以我是一个自学成才的C#程序员。我对C#的所有知识都来自于在学校学习Java并将其应用于C#。我对类的了解并不是很好,使用JSON数据几乎完全没有。所以我有这个JSON数据,我从一个HttpWebClient下拉,然后使用JSON.NET反序列化它。我为类结构使用了json2csharp类生成器。不过,我相信丢失一个导致对象只对所有内容都为空的步骤。C#抽搐类总是存储null
例JSON数据 https://github.com/justintv/Twitch-API/blob/master/v3_resources/games.md GET /游戏/顶部示例数据
我的代码
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;我只是不知道如何访问这个。
抽搐返回分类
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; }
}
}
我相信,我在做什么错误从类和控制台声明来临的时候,我只是不知道,足以做到这一点。尽管我知道这很可能非常简单。
你调试过吗?当你传递给'DeserializeObject'方法时'content'是否有价值? – Leron
是的内容有一个值,该值与给出的示例相似,但是其中包含更多项目,因为它是一个实时样本。 –