2017-03-15 34 views
-1

這是一個名爲TwitchAPIexample的文件,位於項目MyFirstBot下的文件夾插件中。類和代碼如下:C#Beginner - 使用不同於不同類的類

using System.Net; 
using System.IO; 
using Newtonsoft.Json; 

namespace MyFirstBot.Plugins 
{ 
    public class TwitchAPIexample 
    { 

     private const string url = "https://api.twitch.tv/kraken/streams/<channel>"; 

     public bool isTwitchLive; 

     private static void BuildConnect() 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

      request.Method = "Get"; 
      request.Timeout = 12000; 
      request.ContentType = "application/json"; 
      request.Headers.Add("authorization", "<token>"); 

      using (System.IO.Stream s = request.GetResponse().GetResponseStream()) 
      { 
       using (StreamReader sr = new System.IO.StreamReader(s)) 
       { 
        var jsonResponse = sr.ReadToEnd(); 
        RootObject r = JsonConvert.DeserializeObject<RootObject>(jsonResponse); 
       } 
      } 
     } 


     public class Preview 
     { 
      public string small { get; set; } 
      public string medium { get; set; } 
      public string large { get; set; } 
      public string template { get; set; } 
     } 

     public class Channel 
     { 
      public bool mature { get; set; } 
      public string status { get; set; } 
      public string broadcaster_language { get; set; } 
      public string display_name { get; set; } 
      public string game { get; set; } 
      public string language { get; set; } 
      public int _id { get; set; } 
      public string name { get; set; } 
      public string created_at { get; set; } 
      public string updated_at { get; set; } 
      public bool partner { get; set; } 
      public string logo { get; set; } 
      public string video_banner { get; set; } 
      public string profile_banner { get; set; } 
      public object profile_banner_background_color { get; set; } 
      public string url { get; set; } 
      public int views { get; set; } 
      public int followers { get; set; } 
     } 

     public class Stream 
     { 
      public long _id { get; set; } 
      public string game { get; set; } 
      public int viewers { get; set; } 
      public int video_height { get; set; } 
      public int average_fps { get; set; } 
      public int delay { get; set; } 
      public string created_at { get; set; } 
      public bool is_playlist { get; set; } 
      public Preview preview { get; set; } 
      public Channel channel { get; set; } 
     } 

     public class RootObject 
     { 
      public Stream stream { get; set; } 
     } 


    } 

} 

我需要做的是使用在命名空間MyfirstBot.Plugins類下MyFirstBot項目文件不同的文件。我有:

using namespace MyFirstBot.Plugins 

但我不知道如何使用RootObject。我已經嘗試使用:

TwitchAPIexample.stream TwitchLive = new TwitchAPIexample.stream() 

,但我真的不知道如何從那裏去檢查的JSON其他字符串,將它們設置等於字符串,基本上只是如何操縱在TwitchAPIexample課堂上的一切。

再次,我是C#Noob所以你不必爲我寫,但如果你能解釋它或打我一個很好的資源。我GOOGLE了,仍然困惑。 OOP不是我的強項。

這是據我已經得到了:

namespace MyFirstBot 
{ 
    public class DiscordBot 
    { 
     DiscordClient client; 
     CommandService commands; 
     TwitchClient TwitchClient; 
     TwitchAPIexample.Stream TwitchLive = new TwitchAPIexample.Stream(); 
     public DiscordBot() 
     { 
      if(TwitchLive.equals(null)) 
      { 
       //stream is offline 
      } 
     } 
    } 
} 

我不知道這是最好的方法。

+0

你'TwitchAPIexample'類不訪問根對象有一個名爲'Stream'的方法,所以這行代碼沒有意義:'new TwitchAPIexample.Stream()'。你想要訪問哪個流? –

+1

@IsaacKleinman - 'Stream'是'TwitchAPIexample'的嵌套類。在這個例子的背景下,這個用法很有意義,因爲它正在創建一個新的'Stream'實例。 –

+0

爲了實現我正在猜測的你想要實現的目標:改變'BuildConnect'方法返回'rootObject',然後在'DiscordBot'類中調用'BuildConnect()'以獲得json響應的細節。 –

回答

0

對我來說,看起來你需要改變你的架構。您不需要靜態方法,並且需要創建可以訪問RootObject的屬性。而且你並不需要嵌套這些類。

public class TwitchAPIexample 
{ 

    private const string url = "https://api.twitch.tv/kraken/streams/<channel>"; 

    public bool IsTwitchLive { get; set; } 
    public RootObject Root { get; set; } 

    public TwitchAPIexample() 
    { 
     BuildConnect(); 
    } 

    private void BuildConnect() 
    { 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

     request.Method = "Get"; 
     request.Timeout = 12000; 
     request.ContentType = "application/json"; 
     request.Headers.Add("authorization", "<token>"); 

     using (System.IO.Stream s = request.GetResponse().GetResponseStream()) 
     { 
      using (StreamReader sr = new System.IO.StreamReader(s)) 
      { 
       var jsonResponse = sr.ReadToEnd(); 
       this.Root = JsonConvert.DeserializeObject<RootObject>(jsonResponse); 
      } 
     } 
    } 
} 

public class Preview 
{ 
    public string small { get; set; } 
    public string medium { get; set; } 
    public string large { get; set; } 
    public string template { get; set; } 
} 

public class Channel 
{ 
    public bool mature { get; set; } 
    public string status { get; set; } 
    public string broadcaster_language { get; set; } 
    public string display_name { get; set; } 
    public string game { get; set; } 
    public string language { get; set; } 
    public int _id { get; set; } 
    public string name { get; set; } 
    public string created_at { get; set; } 
    public string updated_at { get; set; } 
    public bool partner { get; set; } 
    public string logo { get; set; } 
    public string video_banner { get; set; } 
    public string profile_banner { get; set; } 
    public object profile_banner_background_color { get; set; } 
    public string url { get; set; } 
    public int views { get; set; } 
    public int followers { get; set; } 
} 

public class Stream 
{ 
    public long _id { get; set; } 
    public string game { get; set; } 
    public int viewers { get; set; } 
    public int video_height { get; set; } 
    public int average_fps { get; set; } 
    public int delay { get; set; } 
    public string created_at { get; set; } 
    public bool is_playlist { get; set; } 
    public Preview preview { get; set; } 
    public Channel channel { get; set; } 
} 

public class RootObject 
{ 
    public Stream stream { get; set; } 
} 

現在你可以做下一個

namespace MyFirstBot 
{ 
    public class DiscordBot 
    { 
     DiscordClient client; 
     CommandService commands; 
     TwitchClient TwitchClient; 
     TwitchAPIexample twitchLive = new TwitchAPIexample(); 
     public DiscordBot() 
     { 
      if(twitchLive.Root == null || twitchLive.Root.Stream == null) 
      { 
       //stream is offline 
      } 
     } 
    } 
} 

所以,你正在使用的twitchLive.Root和波谷的根源您可以訪問流twitchLive.Root.Stream