所以我是一個自學成才的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
是的內容有一個值,該值與給出的示例相似,但是其中包含更多項目,因爲它是一個實時樣本。 –