-3

Currently trying to make a system that will change a button's color based on if the streamer on the button is live or not. I have a way to download the json string into a variable but I don't know what to do with that. I know I have to check if the variable "stream" in the json output is null which means the streamer is offline but I have 0 clue on how to do that.

I'll edit it with the code that I currently have. I got the json being properly parsed, doing r.stream gives me the appropriate data, but I can't figure out how to figure out if the stream is live or not. This is supposed to check on button press which will refresh the data.

    private void Refresh_Click(object sender, RoutedEventArgs e)
    {
        string url = @"https://api.twitch.tv/kraken/streams/camoduck?client_id=xskte44y2wfqin464ayecyc09nikcj";

        var json = new WebClient().DownloadString(url);

        Rootobject r = JsonConvert.DeserializeObject<Rootobject>(json);
        Console.WriteLine(r.stream);
        if r.stream.game = "Grand Theft Auto V"
        {
            _1GUnit1.Background = Brushes.Red;
        }


    }
  • Use this unknown site. https://www.google.com/search?q=c%23+how+to+parse+a+json – L.B Aug 10 '17 at 19:40
  • I've spent that last few hours googling different ways that aren't working for me. @L.B – Matt Artist Aug 10 '17 at 19:45
  • `that aren't working for me`. You think you have a very special case? Then show your code. – L.B Aug 10 '17 at 19:48
  • @MattArtist From your code sample it looks like you're lacking a basic understanding of C#. I recommend you brush up on the basics before attempting to do something more advanced like working with a web API. Samples and documentation you find will likely make more sense then. – Logarr Aug 10 '17 at 20:16

3 Answers3

2
.......
var json = new WebClient().DownloadString(url);
var r = JsonConvert.DeserializeObject<Rootobject>(json); 
Console.WriteLine(r.stream);
if (r.stream==null) //How a null check can be done
{
    _1GUnit1.Background = Brushes.Red;
}

BTW: If you are using "http://json2csharp.com/", It is propably RootObject not Rootobject

EZI
  • 15,209
  • 2
  • 27
  • 33
  • This worked amazingly. I'm brand new to C#, probably should've mentioned that, and I was trying to figure out how to compare to a null. +1 thanks. – Matt Artist Aug 10 '17 at 20:15
1

Without you providing more details about what doesn't work in your case, I can't give an in-depth explanation or guidance.

What I can suggest is that you a use an API wrapper, e.g. TwitchLib. That should help you get started and should provide enough documentation for your case.

pankee
  • 341
  • 1
  • 2
  • 12
0

You should do more reading on the Twitch API and search for examples. When the stream is offline, consider dropping in some sort of template as there is no data to from the request to parse. When a stream is online, you will have access to the stream object's properties. For example, if your success function returns data, you can assign the results as:

game = data.stream.game;
logo = data.stream.channel.logo;
name = data.stream.channel.name;
url = data.stream.channel.url;
stream = data.stream.stream_type;

This assumes you've setup the appropriate variables (you didn't provide any code).

I would also recommend you spend some time learning how to debug in the browser. More specifically in this case, learn how to inspect your result data. This will demystify what's in the object as you'll see the data and its properties, etc.

Have a look at the following Stack post: Inspecting large JSON data in Chrome