2016-03-28 46 views
0

所以我是一个自学成才的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; } 
    } 

} 

我相信,我在做什么错误从类和控制台声明来临的时候,我只是不知道,足以做到这一点。尽管我知道这很可能非常简单。

+0

你调试过吗?当你传递给'DeserializeObject'方法时'content'是否有价值? – Leron

+0

是的内容有一个值,该值与给出的示例相似,但是其中包含更多项目,因为它是一个实时样本。 –

回答

0

仅当.Net实体与传入的JSON数据明显匹配时,才可以将JSON转换为.NET对象反序列化。在这里,您不必创建嵌套类,而必须创建TwitchReturn类,并根据数据决定属性,以及它是否会收集。

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

谢谢你为我清理。我也没有改变打印出来的代码来说,Console.WriteLine(objectHolder.top.ToList()[0] .game.name); –

+0

这工作正常,我非常感谢你为我回答这个问题。关于学习Twitches API所有这些功能的最难的部分是,似乎没有人使用C#来查看示例。 –