2

I am using twitch api json files with javascript functions and I wanted to see if I could write an if else statement with a part of the json file being tested. Here is the link to the json file.

So now that we have the json file let me explain what I want it to do. I want to select the "stream" part of the json file and want to test it in a javascript. If "stream" == null then say Offline but if "stream" != nul then say online.

Here is the function I have so far:

  $.getJSON("https://api.twitch.tv/kraken/streams/"+ twitchName +".json?callback=?", function(c){
    if (stream == null) {
      document.getElementById("live").innerHTML="Offline";
    };
    else{
      document.getElementById("live").innerHTML="Online";
    }
  });

What am I doing wrong? Can someone please help? Thanks.

2
  • 5
    if(c.stream ==null)... c is the response object as you have defined it in callback Commented Dec 30, 2013 at 1:29
  • @charlietfl that's an answer — post it as such for acceptance! Commented Dec 30, 2013 at 1:33

1 Answer 1

2

Try this:

$.getJSON("https://api.twitch.tv/kraken/streams/"+ twitchName +".json?callback=?", function(c){
  if (c.stream == null) {
    $("#live").html("Offline");
  } else {
    $("#live").html("Online");
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I am still new to javascript I really appreciate the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.